
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>表达式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="05_Statements.html" />
    
    
    <link rel="prev" href="03_Types.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/01_The_Basics.html">
            
                <a href="../chapter2/01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/02_Basic_Operators.html">
            
                <a href="../chapter2/02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/03_Strings_and_Characters.html">
            
                <a href="../chapter2/03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/04_Collection_Types.html">
            
                <a href="../chapter2/04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/05_Control_Flow.html">
            
                <a href="../chapter2/05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../chapter2/06_Functions.html">
            
                <a href="../chapter2/06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../chapter2/07_Closures.html">
            
                <a href="../chapter2/07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../chapter2/08_Enumerations.html">
            
                <a href="../chapter2/08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../chapter2/09_Structures_And_Classes.html">
            
                <a href="../chapter2/09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../chapter2/10_Properties.html">
            
                <a href="../chapter2/10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../chapter2/11_Methods.html">
            
                <a href="../chapter2/11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../chapter2/12_Subscripts.html">
            
                <a href="../chapter2/12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../chapter2/13_Inheritance.html">
            
                <a href="../chapter2/13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../chapter2/14_Initialization.html">
            
                <a href="../chapter2/14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../chapter2/15_Deinitialization.html">
            
                <a href="../chapter2/15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../chapter2/16_Optional_Chaining.html">
            
                <a href="../chapter2/16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../chapter2/17_Error_Handling.html">
            
                <a href="../chapter2/17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../chapter2/18_Type_Casting.html">
            
                <a href="../chapter2/18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../chapter2/19_Nested_Types.html">
            
                <a href="../chapter2/19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../chapter2/20_Extensions.html">
            
                <a href="../chapter2/20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../chapter2/21_Protocols.html">
            
                <a href="../chapter2/21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../chapter2/22_Generics.html">
            
                <a href="../chapter2/22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../chapter2/23_Automatic_Reference_Counting.html">
            
                <a href="../chapter2/23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../chapter2/24_Memory_Safety.html">
            
                <a href="../chapter2/24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="../chapter2/25_Access_Control.html">
            
                <a href="../chapter2/25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="../chapter2/26_Advanced_Operators.html">
            
                <a href="../chapter2/26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="01_About_the_Language_Reference.html">
            
                <a href="01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="02_Lexical_Structure.html">
            
                <a href="02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="03_Types.html">
            
                <a href="03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.4" data-path="04_Expressions.html">
            
                <a href="04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="05_Statements.html">
            
                <a href="05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="06_Declarations.html">
            
                <a href="06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="07_Attributes.html">
            
                <a href="07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="08_Patterns.html">
            
                <a href="08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="09_Generic_Parameters_and_Arguments.html">
            
                <a href="09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="10_Summary_of_the_Grammar.html">
            
                <a href="10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >表达式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8868;&#x8FBE;&#x5F0F;&#xFF08;expressions&#xFF09;">&#x8868;&#x8FBE;&#x5F0F;&#xFF08;Expressions&#xFF09;</h1>
<p>Swift &#x4E2D;&#x5B58;&#x5728;&#x56DB;&#x79CD;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x503C;&#x7684;&#x540C;&#x65F6;&#x8FD8;&#x53EF;&#x4EE5;&#x5F15;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x5BF9;&#x7B80;&#x5355;&#x8868;&#x8FBE;&#x5F0F;&#x4F7F;&#x7528;&#x5404;&#x79CD;&#x8FD0;&#x7B97;&#x7B26;&#x3002;&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;&#x4ECE;&#x6982;&#x5FF5;&#x4E0A;&#x8BB2;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x4E00;&#x79CD;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x79CD;&#x8BBF;&#x95EE;&#x503C;&#x7684;&#x65B9;&#x5F0F;&#x3002;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x5219;&#x5141;&#x8BB8;&#x4F60;&#x5EFA;&#x7ACB;&#x590D;&#x6742;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x548C;&#x6210;&#x5458;&#x8BBF;&#x95EE;&#x3002;&#x6BCF;&#x79CD;&#x8868;&#x8FBE;&#x5F0F;&#x90FD;&#x5728;&#x4E0B;&#x9762;&#x6709;&#x8BE6;&#x7EC6;&#x8BBA;&#x8FF0;&#x3002;</p>
<blockquote>
<p>&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="expression">expression </h4>
<blockquote>
<p><em>&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#try-operator"><em>try &#x8FD0;&#x7B97;&#x7B26;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#prefix-expression"><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="#binary-expressions"><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em></a><sub>&#x53EF;&#x9009;</sub></p>
</blockquote>
<h4 id="expression-list">expression-list </h4>
<blockquote>
<p><em>&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em> &#x2192; <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> | <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>,</strong> <a href="#expression-list"><em>&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h2 id="prefix-expressions">&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F; </h2>
<p>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x7531;&#x53EF;&#x9009;&#x7684;&#x524D;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;&#x548C;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#x3002;&#x524D;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;&#x53EA;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x8FBE;&#x5F0F;&#x5219;&#x7D27;&#x968F;&#x5176;&#x540E;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x8FD9;&#x4E9B;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/02_Basic_Operators.html">&#x57FA;&#x672C;&#x8FD0;&#x7B97;&#x7B26;</a> &#x548C; <a href="../chapter2/26_Advanced_Operators.html">&#x9AD8;&#x7EA7;&#x8FD0;&#x7B97;&#x7B26;</a>&#x3002;</p>
<p>&#x5173;&#x4E8E; Swift &#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/operator_declarations" target="_blank"><em>Operators Declarations</em></a>&#x3002;</p>
<p>&#x9664;&#x4E86;&#x6807;&#x51C6;&#x5E93;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5BF9;&#x67D0;&#x4E2A;&#x53D8;&#x91CF;&#x4F7F;&#x7528; <code>&amp;</code> &#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x4ECE;&#x800C;&#x5C06;&#x5176;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x7684;&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x53C2;&#x6570;&#x3002;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/06_Functions.html#in_out_parameters">&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x53C2;&#x6570;</a>&#x3002;</p>
<blockquote>
<p>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="prefix-expression">prefix-expression </h4>
<blockquote>
<p><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="02_Lexical_Structure.html#prefix-operator"><em>&#x524D;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#in-out-expression"><em>&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h4 id="in-out-expression">in-out-expression </h4>
<blockquote>
<p><em>&#x8F93;&#x5165;&#x8F93;&#x51FA;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>&amp;</strong> <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a></p>
</blockquote>
<h3 id="try-operator">Try &#x8FD0;&#x7B97;&#x7B26; </h3>
<p>try &#x8868;&#x8FBE;&#x5F0F;&#x7531; <code>try</code> &#x8FD0;&#x7B97;&#x7B26;&#x52A0;&#x4E0A;&#x7D27;&#x968F;&#x5176;&#x540E;&#x7684;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>try <code>&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;</code></p>
</blockquote>
<p>&#x53EF;&#x9009;&#x7684; try &#x8868;&#x8FBE;&#x5F0F;&#x7531; <code>try?</code> &#x8FD0;&#x7B97;&#x7B26;&#x52A0;&#x4E0A;&#x7D27;&#x968F;&#x5176;&#x540E;&#x7684;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>try? <code>&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;</code></p>
</blockquote>
<p>&#x5982;&#x679C;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x6CA1;&#x6709;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#xFF0C;&#x6574;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x8FD4;&#x56DE;&#x7684;&#x53EF;&#x9009;&#x503C;&#x5C06;&#x5305;&#x542B;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5426;&#x5219;&#xFF0C;&#x8BE5;&#x53EF;&#x9009;&#x503C;&#x4E3A; <code>nil</code>&#x3002;</p>
<p>&#x5F3A;&#x5236;&#x7684; try &#x8868;&#x8FBE;&#x5F0F;&#x7531; <code>try!</code> &#x8FD0;&#x7B97;&#x7B26;&#x52A0;&#x4E0A;&#x7D27;&#x968F;&#x5176;&#x540E;&#x7684;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>try! <code>&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;</code></p>
</blockquote>
<p>&#x5982;&#x679C;&#x53EF;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x629B;&#x51FA;&#x4E86;&#x9519;&#x8BEF;&#xFF0C;&#x5C06;&#x4F1A;&#x5F15;&#x53D1;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x5728;&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;&#x5DE6;&#x4FA7;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x88AB;&#x6807;&#x8BB0;&#x4E0A; <code>try</code>&#x3001;<code>try?</code> &#x6216;&#x8005; <code>try!</code> &#x65F6;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FD0;&#x7B97;&#x7B26;&#x5BF9;&#x6574;&#x4E2A;&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x90FD;&#x4EA7;&#x751F;&#x4F5C;&#x7528;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x6765;&#x660E;&#x786E;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x4F5C;&#x7528;&#x8303;&#x56F4;&#x3002;</p>
<pre><code class="lang-swift">sum = <span class="hljs-keyword">try</span> someThrowingFunction() + anotherThrowingFunction()   <span class="hljs-comment">// try &#x5BF9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x90FD;&#x4EA7;&#x751F;&#x4F5C;&#x7528;</span>
sum = <span class="hljs-keyword">try</span> (someThrowingFunction() + anotherThrowingFunction()) <span class="hljs-comment">// try &#x5BF9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x90FD;&#x4EA7;&#x751F;&#x4F5C;&#x7528;</span>
sum = (<span class="hljs-keyword">try</span> someThrowingFunction()) + anotherThrowingFunction() <span class="hljs-comment">// &#x9519;&#x8BEF;&#xFF1A;try &#x53EA;&#x5BF9;&#x7B2C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4EA7;&#x751F;&#x4F5C;&#x7528;</span>
</code></pre>
<p><code>try</code> &#x8868;&#x8FBE;&#x5F0F;&#x4E0D;&#x80FD;&#x51FA;&#x73B0;&#x5728;&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x7684;&#x53F3;&#x4FA7;&#xFF0C;&#x9664;&#x975E;&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;&#x662F;&#x8D4B;&#x503C;&#x8FD0;&#x7B97;&#x7B26;&#x6216;&#x8005; <code>try</code> &#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x88AB;&#x5706;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#x3002;</p>
<p>&#x5173;&#x4E8E; <code>try</code>&#x3001;<code>try?</code> &#x548C; <code>try!</code> &#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x4EE5;&#x53CA;&#x8BE5;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/17_Error_Handling.html">&#x9519;&#x8BEF;&#x5904;&#x7406;</a>&#x3002;</p>
<blockquote>
<p>Try &#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="try-operator">try-operator </h4>
<blockquote>
<p><em>try &#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>try</strong> | <strong>try?</strong> | <strong>try!</strong></p>
</blockquote>
<h2 id="binary-expressions">&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F; </h2>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em>&#x7531;&#x4E2D;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;&#x548C;&#x5DE6;&#x53F3;&#x53C2;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#x3002;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x5DE6;&#x4FA7;&#x53C2;&#x6570;</code> <code>&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;</code> <code>&#x53F3;&#x4FA7;&#x53C2;&#x6570;</code></p>
</blockquote>
<p>&#x5173;&#x4E8E;&#x8FD9;&#x4E9B;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/02_Basic_Operators.html">&#x57FA;&#x672C;&#x8FD0;&#x7B97;&#x7B26;</a> &#x548C; <a href="../chapter2/26_Advanced_Operators.html">&#x9AD8;&#x7EA7;&#x8FD0;&#x7B97;&#x7B26;</a>&#x3002;</p>
<p>&#x5173;&#x4E8E; Swift &#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/operator_declarations" target="_blank"><em>Swift Standard Library Operators Reference</em></a>&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5728;&#x89E3;&#x6790;&#x65F6;&#xFF0C;&#x4E00;&#x4E2A;&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x5C06;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6241;&#x5E73;&#x5217;&#x8868;&#x8868;&#x793A;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x518D;&#x8FDB;&#x4E00;&#x6B65;&#x8FDB;&#x884C;&#x7EC4;&#x5408;&#x3002;&#x4F8B;&#x5982;&#xFF0C;<code>2 + 3 * 5</code> &#x9996;&#x5148;&#x88AB;&#x770B;&#x4F5C;&#x5177;&#x6709;&#x4E94;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x5373; <code>2</code>&#x3001;<code>+</code>&#x3001;<code>3</code>&#x3001;<code>*</code>&#x3001;<code>5</code>&#xFF0C;&#x968F;&#x540E;&#x6839;&#x636E;&#x8FD0;&#x7B97;&#x7B26;&#x4F18;&#x5148;&#x7EA7;&#x7EC4;&#x5408;&#x4E3A; <code>(2 + (3 * 5))</code>&#x3002;</p>
</blockquote>
<h4 id="binary-expression">binary-expression </h4>
<blockquote>
<p>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="02_Lexical_Structure.html#binary-operator"><em>&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;</em></a> <a href="#prefix-expression"><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#assignment-operator"><em>&#x8D4B;&#x503C;&#x8FD0;&#x7B97;&#x7B26;</em></a> <a href="#try-operator"><em>try &#x8FD0;&#x7B97;&#x7B26;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#prefix-expression"><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#conditional-operator"><em>&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;</em></a> <a href="#try-operator"><em>try &#x8FD0;&#x7B97;&#x7B26;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#prefix-expression"><em>&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#type-casting-operator"><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;</em></a></p>
</blockquote>
<h4 id="binary-expressions">binary-expressions </h4>
<blockquote>
<p><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em> &#x2192; <a href="#binary-expression"><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="#binary-expressions"><em>&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em></a><sub>&#x53EF;&#x9009;</sub></p>
</blockquote>
<h3 id="assignment-operator">&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p>&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x4E3A;&#x67D0;&#x4E2A;&#x7ED9;&#x5B9A;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8D4B;&#x503C;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1B;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code> = <code>&#x503C;</code></p>
</blockquote>
<p>&#x53F3;&#x8FB9;&#x7684;&#x503C;&#x4F1A;&#x88AB;&#x8D4B;&#x503C;&#x7ED9;&#x5DE6;&#x8FB9;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5982;&#x679C;&#x5DE6;&#x8FB9;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#xFF0C;&#x90A3;&#x4E48;&#x53F3;&#x8FB9;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x540C;&#x6837;&#x5143;&#x7D20;&#x4E2A;&#x6570;&#x7684;&#x5143;&#x7EC4;&#x3002;&#xFF08;&#x5D4C;&#x5957;&#x5143;&#x7EC4;&#x4E5F;&#x662F;&#x5141;&#x8BB8;&#x7684;&#x3002;&#xFF09;&#x53F3;&#x8FB9;&#x7684;&#x503C;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x90E8;&#x5206;&#x90FD;&#x4F1A;&#x88AB;&#x8D4B;&#x503C;&#x7ED9;&#x5DE6;&#x8FB9;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684;&#x76F8;&#x5E94;&#x90E8;&#x5206;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift">(a, <span class="hljs-number">_</span>, (b, <span class="hljs-built_in">c</span>)) = (<span class="hljs-string">&quot;test&quot;</span>, <span class="hljs-number">9.45</span>, (<span class="hljs-number">12</span>, <span class="hljs-number">3</span>))
<span class="hljs-comment">// a &#x4E3A; &quot;test&quot;&#xFF0C;b &#x4E3A; 12&#xFF0C;c &#x4E3A; 3&#xFF0C;9.45 &#x4F1A;&#x88AB;&#x5FFD;&#x7565;</span>
</code></pre>
<p>&#x8D4B;&#x503C;&#x8FD0;&#x7B97;&#x7B26;&#x4E0D;&#x8FD4;&#x56DE;&#x4EFB;&#x4F55;&#x503C;&#x3002;</p>
<blockquote>
<p>&#x8D4B;&#x503C;&#x8FD0;&#x7B97;&#x7B26;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="assignment-operator">assignment-operator </h4>
<blockquote>
<p><em>&#x8D4B;&#x503C;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>=</strong></p>
</blockquote>
<h3 id="ternary-conditional-operator">&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26; </h3>
<p><em>&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;</em>&#x4F1A;&#x6839;&#x636E;&#x6761;&#x4EF6;&#x6765;&#x5BF9;&#x4E24;&#x4E2A;&#x7ED9;&#x5B9A;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x6761;&#x4EF6;</code> ? <code>&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x6761;&#x4EF6;&#x4E3A;&#x771F;&#x5219;&#x4F7F;&#x7528;&#xFF09;</code> : <code>&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x6761;&#x4EF6;&#x4E3A;&#x5047;&#x5219;&#x4F7F;&#x7528;&#xFF09;</code></p>
</blockquote>
<p>&#x5982;&#x679C;&#x6761;&#x4EF6;&#x4E3A;&#x771F;&#xFF0C;&#x90A3;&#x4E48;&#x5BF9;&#x7B2C;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#x5E76;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x5BF9;&#x7B2C;&#x4E8C;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#x5E76;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x3002;&#x672A;&#x4F7F;&#x7528;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4E0D;&#x4F1A;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x4F7F;&#x7528;&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/02_Basic_Operators.html#ternary_conditional_operator">&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;</a>&#x3002;</p>
<blockquote>
<p>&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="conditional-operator">conditional-operator </h4>
<blockquote>
<p><em>&#x4E09;&#x5143;&#x6761;&#x4EF6;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>?</strong> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>:</strong></p>
</blockquote>
<h3 id="type-casting-operators">&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26; </h3>
<p>&#x6709; 4 &#x79CD;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;<code>is</code>&#x3001;<code>as</code>&#x3001;<code>as?</code> &#x548C; <code>as!</code>&#x3002;&#x5B83;&#x4EEC;&#x6709;&#x5982;&#x4E0B;&#x7684;&#x5F62;&#x5F0F;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code> is <code>&#x7C7B;&#x578B;</code></p>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code> as <code>&#x7C7B;&#x578B;</code></p>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code> as? <code>&#x7C7B;&#x578B;</code></p>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code> as! <code>&#x7C7B;&#x578B;</code></p>
</blockquote>
<p><code>is</code> &#x8FD0;&#x7B97;&#x7B26;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x68C0;&#x67E5;&#x8868;&#x8FBE;&#x5F0F;&#x80FD;&#x5426;&#x5411;&#x4E0B;&#x8F6C;&#x5316;&#x4E3A;&#x6307;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x5982;&#x679C;&#x53EF;&#x4EE5;&#x5219;&#x8FD4;&#x56DE; <code>ture</code>&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; <code>false</code>&#x3002;</p>
<p><code>as</code> &#x8FD0;&#x7B97;&#x7B26;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x6267;&#x884C;&#x5411;&#x4E0A;&#x8F6C;&#x6362;&#x548C;&#x6865;&#x63A5;&#x3002;&#x5411;&#x4E0A;&#x8F6C;&#x6362;&#x53EF;&#x5C06;&#x8868;&#x8FBE;&#x5F0F;&#x8F6C;&#x6362;&#x6210;&#x7236;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x800C;&#x65E0;&#x9700;&#x4F7F;&#x7528;&#x4EFB;&#x4F55;&#x4E2D;&#x95F4;&#x53D8;&#x91CF;&#x3002;&#x4EE5;&#x4E0B;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">f</span><span class="hljs-params">(any: Any)</span></span> { <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Function for Any&quot;</span>) }
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">f</span><span class="hljs-params">(int: Int)</span></span> { <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Function for Int&quot;</span>) }
<span class="hljs-keyword">let</span> x = <span class="hljs-number">10</span>
f(x)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Function for Int&#x201D;</span>

<span class="hljs-keyword">let</span> y: <span class="hljs-type">Any</span> = x
f(y)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Function for Any&#x201D;</span>

f(x <span class="hljs-keyword">as</span> <span class="hljs-type">Any</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;Function for Any&#x201D;</span>
</code></pre>
<p>&#x6865;&#x63A5;&#x53EF;&#x5C06; Swift &#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#xFF08;&#x4F8B;&#x5982; <code>String</code>&#xFF09;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x4E0E;&#x4E4B;&#x76F8;&#x5173;&#x7684; Foundation &#x7C7B;&#x578B;&#xFF08;&#x4F8B;&#x5982; <code>NSString</code>&#xFF09;&#x6765;&#x4F7F;&#x7528;&#xFF0C;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;&#x5173;&#x4E8E;&#x6865;&#x63A5;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/imported_c_and_objective_c_apis/working_with_foundation_types" target="_blank"><em>Working with Foundation Types</em></a>&#x3002;</p>
<p><code>as?</code> &#x8FD0;&#x7B97;&#x7B26;&#x6709;&#x6761;&#x4EF6;&#x5730;&#x6267;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF0C;&#x8FD4;&#x56DE;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x7684;&#x53EF;&#x9009;&#x503C;&#x3002;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x8F6C;&#x6362;&#x6210;&#x529F;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x53EF;&#x9009;&#x503C;&#x5C06;&#x5305;&#x542B;&#x8F6C;&#x6362;&#x540E;&#x7684;&#x503C;&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;&#x5982;&#x679C;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x5C31;&#x80FD;&#x786E;&#x5B9A;&#x8F6C;&#x6362;&#x4E00;&#x5B9A;&#x4F1A;&#x6210;&#x529F;&#x6216;&#x662F;&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x4F1A;&#x5BFC;&#x81F4;&#x7F16;&#x8BD1;&#x62A5;&#x9519;&#x3002;</p>
<p><code>as!</code> &#x8FD0;&#x7B97;&#x7B26;&#x6267;&#x884C;&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF0C;&#x8FD4;&#x56DE;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x7684;&#x975E;&#x53EF;&#x9009;&#x503C;&#x3002;&#x5982;&#x679C;&#x8F6C;&#x6362;&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x4F1A;&#x5BFC;&#x81F4;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;&#x8868;&#x8FBE;&#x5F0F; <code>x as! T</code> &#x6548;&#x679C;&#x7B49;&#x540C;&#x4E8E; <code>(x as? T)!</code>&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x7684;&#x66F4;&#x591A;&#x5185;&#x5BB9;&#x548C;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/18_Type_Casting.html">&#x7C7B;&#x578B;&#x8F6C;&#x6362;</a>&#x3002;</p>
<h4 id="type-casting-operator">type-casting-operator </h4>
<blockquote>
<p>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;&#x8BED;&#x6CD5;</p>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>is</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>as</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>as</strong> <strong>?</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8FD0;&#x7B97;&#x7B26;</em> &#x2192; <strong>as</strong> <strong>!</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
</blockquote>
<h2 id="primary-expressions">&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F; </h2>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em>&#x662F;&#x6700;&#x57FA;&#x672C;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5B83;&#x4EEC;&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x4F7F;&#x7528;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8DDF;&#x524D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x3001;&#x4E8C;&#x5143;&#x8868;&#x8FBE;&#x5F0F;&#x3001;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x5408;&#x4F7F;&#x7528;&#x3002;</p>
<blockquote>
<p>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="primary-expression">primary-expression </h4>
<blockquote>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a> <a href="09_Generic_Parameters_and_Arguments.html#generic-argument-clause"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;</em></a><sub>&#x53EF;&#x9009;</sub></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#literal-expression"><em>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#self-expression"><em>self &#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#superclass-expression"><em>&#x7236;&#x7C7B;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#closure-expression"><em>&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#parenthesized-expression"><em>&#x5706;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#implicit-member-expression"><em>&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#wildcard-expression"><em>&#x901A;&#x914D;&#x7B26;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#selector-expression"><em>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#key-patch-string-expression"><em>key-path&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h3 id="literal-expression">&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;</em>&#x53EF;&#x7531;&#x666E;&#x901A;&#x5B57;&#x9762;&#x91CF;&#xFF08;&#x4F8B;&#x5982;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x8005;&#x6570;&#x5B57;&#xFF09;&#xFF0C;&#x5B57;&#x5178;&#x6216;&#x8005;&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#xFF0C;&#x6216;&#x8005;&#x4E0B;&#x9762;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x7279;&#x6B8A;&#x5B57;&#x9762;&#x91CF;&#x7EC4;&#x6210;&#xFF1A;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5B57;&#x9762;&#x91CF;</th>
<th style="text-align:left">&#x7C7B;&#x578B;</th>
<th style="text-align:left">&#x503C;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>#file</code></td>
<td style="text-align:left"><code>String</code></td>
<td style="text-align:left">&#x6240;&#x5728;&#x7684;&#x6587;&#x4EF6;&#x540D;</td>
</tr>
<tr>
<td style="text-align:left"><code>#line</code></td>
<td style="text-align:left"><code>Int</code></td>
<td style="text-align:left">&#x6240;&#x5728;&#x7684;&#x884C;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left"><code>#column</code></td>
<td style="text-align:left"><code>Int</code></td>
<td style="text-align:left">&#x6240;&#x5728;&#x7684;&#x5217;&#x6570;</td>
</tr>
<tr>
<td style="text-align:left"><code>#function</code></td>
<td style="text-align:left"><code>String</code></td>
<td style="text-align:left">&#x6240;&#x5728;&#x7684;&#x58F0;&#x660E;&#x7684;&#x540D;&#x5B57;</td>
</tr>
</tbody>
</table>
<p>&#x5BF9;&#x4E8E; <code>&#xFF03;function</code>&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x4F1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5728;&#x65B9;&#x6CD5;&#x4E2D;&#x4F1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x65B9;&#x6CD5;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5728;&#x5C5E;&#x6027;&#x7684;&#x5B58;&#x53D6;&#x5668;&#x4E2D;&#x4F1A;&#x8FD4;&#x56DE;&#x5C5E;&#x6027;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5728;&#x7279;&#x6B8A;&#x7684;&#x6210;&#x5458;&#x5982; <code>init</code> &#x6216; <code>subscript</code> &#x4E2D;&#x4F1A;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x5728;&#x67D0;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x4F1A;&#x8FD4;&#x56DE;&#x5F53;&#x524D;&#x6A21;&#x5757;&#x7684;&#x540D;&#x5B57;&#x3002;</p>
<p>&#x5F53;&#x5176;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x7684;&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x503C;&#x65F6;&#xFF0C;&#x8BE5;&#x5B57;&#x9762;&#x91CF;&#x7684;&#x503C;&#x53D6;&#x51B3;&#x4E8E;&#x51FD;&#x6570;&#x6216;&#x65B9;&#x6CD5;&#x7684;&#x8C03;&#x7528;&#x73AF;&#x5883;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">logFunctionName</span><span class="hljs-params">(string: String = #function)</span></span> {
    <span class="hljs-built_in">print</span>(string)
}
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">myFunction</span><span class="hljs-params">()</span></span> {
    logFunctionName()
}
myFunction() <span class="hljs-comment">// &#x6253;&#x5370;&#x201C;myFunction()&#x201D;</span>
</code></pre>
<p>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x662F;&#x503C;&#x7684;&#x6709;&#x5E8F;&#x96C6;&#x5408;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>[<code>&#x503C; 1</code>, <code>&#x503C; 2</code>, <code>...</code>]</p>
</blockquote>
<p>&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x7D27;&#x8DDF;&#x4E00;&#x4E2A;&#x9017;&#x53F7;&#x3002;&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#x662F; <code>[T]</code>&#xFF0C;&#x8FD9;&#x4E2A; <code>T</code> &#x5C31;&#x662F;&#x6570;&#x7EC4;&#x4E2D;&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x6570;&#x7EC4;&#x4E2D;&#x5305;&#x542B;&#x591A;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;<code>T</code> &#x5219;&#x662F;&#x8DDF;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x6700;&#x8FD1;&#x7684;&#x7684;&#x516C;&#x5171;&#x7236;&#x7C7B;&#x578B;&#x3002;&#x7A7A;&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x7531;&#x4E00;&#x7EC4;&#x65B9;&#x62EC;&#x53F7;&#x5B9A;&#x4E49;&#xFF0C;&#x53EF;&#x7528;&#x6765;&#x521B;&#x5EFA;&#x7279;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x7A7A;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> emptyArray: [<span class="hljs-type">Double</span>] = []
</code></pre>
<p>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x65E0;&#x5E8F;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>[<code>&#x952E; 1</code> : <code>&#x503C; 1</code>, <code>&#x952E; 2</code> : <code>&#x503C; 2</code>, <code>...</code>]</p>
</blockquote>
<p>&#x5B57;&#x5178;&#x4E2D;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x7D27;&#x8DDF;&#x4E00;&#x4E2A;&#x9017;&#x53F7;&#x3002;&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#x662F; <code>[Key : Value]</code>&#xFF0C;<code>Key</code> &#x8868;&#x793A;&#x952E;&#x7684;&#x7C7B;&#x578B;&#xFF0C;<code>Value</code> &#x8868;&#x793A;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x5B57;&#x5178;&#x4E2D;&#x5305;&#x542B;&#x591A;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48; <code>Key</code> &#x8868;&#x793A;&#x7684;&#x7C7B;&#x578B;&#x5219;&#x4E3A;&#x6240;&#x6709;&#x952E;&#x6700;&#x63A5;&#x8FD1;&#x7684;&#x516C;&#x5171;&#x7236;&#x7C7B;&#x578B;&#xFF0C;<code>Value</code> &#x4E0E;&#x4E4B;&#x76F8;&#x4F3C;&#x3002;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x7531;&#x65B9;&#x62EC;&#x53F7;&#x4E2D;&#x52A0;&#x4E00;&#x4E2A;&#x5192;&#x53F7;&#x7EC4;&#x6210;&#xFF08;<code>[:]</code>&#xFF09;&#xFF0C;&#x4ECE;&#x800C;&#x4E0E;&#x7A7A;&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x533A;&#x5206;&#x5F00;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7A7A;&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x6765;&#x521B;&#x5EFA;&#x7279;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x5178;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> emptyDictionary: [<span class="hljs-type">String</span> : <span class="hljs-type">Double</span>] = [:]
</code></pre>
<p>Xcode &#x4F7F;&#x7528; playground &#x5B57;&#x9762;&#x91CF;&#x5BF9;&#x7A0B;&#x5E8F;&#x7F16;&#x8F91;&#x5668;&#x4E2D;&#x7684;&#x989C;&#x8272;&#x3001;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x56FE;&#x7247;&#x521B;&#x5EFA;&#x53EF;&#x4EA4;&#x4E92;&#x7684;&#x5C55;&#x793A;&#x3002;&#x5728; Xcode &#x4E4B;&#x5916;&#x7684;&#x7A7A;&#x767D;&#x6587;&#x672C;&#x4E2D;&#xFF0C;playground &#x5B57;&#x9762;&#x91CF;&#x4F7F;&#x7528;&#x4E00;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x5B57;&#x9762;&#x91CF;&#x8BED;&#x6CD5;&#x6765;&#x5C55;&#x793A;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x5728; Xcode &#x4E2D;&#x4F7F;&#x7528; playground &#x5B57;&#x9762;&#x91CF;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://help.apple.com/xcode/mac/current/#/dev4c60242fc" target="_blank">&#x6DFB;&#x52A0;&#x989C;&#x8272;&#x3001;&#x6587;&#x4EF6;&#x6216;&#x56FE;&#x7247;&#x5B57;&#x9762;&#x91CF;</a>&#x3002;</p>
<blockquote>
<p>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<h4 id="literal-expression">literal-expression </h4>
<p><em>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="02_Lexical_Structure.html#literal"><em>&#x5B57;&#x9762;&#x91CF;</em></a></p>
<p><em>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#array-literal"><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;</em></a> | <a href="#dictionary-literal"><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;</em></a> | <a href="#playground-literal"><em>&#x7EC3;&#x4E60;&#x573A;&#x5B57;&#x9762;&#x91CF;</em></a></p>
<p><em>&#x5B57;&#x9762;&#x91CF;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>#file</strong> | <strong>#line</strong> | <strong>#column</strong> | <strong>#function</strong></p>
<h4 id="array-literal">array-literal </h4>
<p><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;</em> &#x2192; [<a href="#array-literal-items"><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>]</strong></p>
<h4 id="array-literal-items">array-literal-items </h4>
<p><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em> &#x2192; <a href="#array-literal-item"><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;</em></a> <strong>,</strong><sub>&#x53EF;&#x9009;</sub> | <a href="#array-literal-item"><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;</em></a> <strong>,</strong> <a href="#array-literal-items"><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em></a></p>
<h4 id="array-literal-item">array-literal-item </h4>
<p><em>&#x6570;&#x7EC4;&#x5B57;&#x9762;&#x91CF;&#x9879;</em> &#x2192; <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<h4 id="dictionary-literal">dictionary-literal </h4>
<p><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;</em> &#x2192; [<a href="#dictionary-literal-items"><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em></a> <strong>]</strong> | <strong>[</strong> <strong>:</strong> <strong>]</strong></p>
<h4 id="dictionary-literal-items">dictionary-literal-items </h4>
<p><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em> &#x2192; <a href="#dictionary-literal-item"><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;</em></a> <strong>,</strong><sub>&#x53EF;&#x9009;</sub> | <a href="#dictionary-literal-item"><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;</em></a> <strong>,</strong> <a href="#dictionary-literal-items"><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;&#x5217;&#x8868;</em></a></p>
<h4 id="dictionary-literal-item">dictionary-literal-item </h4>
<p><em>&#x5B57;&#x5178;&#x5B57;&#x9762;&#x91CF;&#x9879;</em> &#x2192; <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>:</strong> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a>&#x3002;</p>
<h4 id="playground-literal">playground-literal </h4>
<p><em>playground &#x5B57;&#x9762;&#x91CF;</em> &#x2192; <strong>#colorLiteral ( red :  <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> , green :<a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="#e[*&#x8868;&#x8FBE;&#x5F0F;*](#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> xpression) ,  blue :<a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> , alpha : <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> )</strong></p>
<p><em>playground &#x5B57;&#x9762;&#x91CF;</em> &#x2192; <strong>#fileLiteral ( resourceName : <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> )</strong></p>
<h4 id="self-expression">playground &#x5B57;&#x9762;&#x91CF;<em> &#x2192; **#imageLiteral ( resourceName : [</em>&#x8868;&#x8FBE;&#x5F0F;<em>](#expression) )*</em>self_expression </h4>
</blockquote>
<h3 id="self-&#x8868;&#x8FBE;&#x5F0F;">Self &#x8868;&#x8FBE;&#x5F0F;</h3>
<p><code>self</code> &#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x5BF9;&#x5F53;&#x524D;&#x7C7B;&#x578B;&#x6216;&#x8005;&#x5F53;&#x524D;&#x5B9E;&#x4F8B;&#x7684;&#x663E;&#x5F0F;&#x5F15;&#x7528;&#xFF0C;&#x5B83;&#x6709;&#x5982;&#x4E0B;&#x5F62;&#x5F0F;&#xFF1A;</p>
<blockquote>
<p>self</p>
<p>self.<code>&#x6210;&#x5458;&#x540D;&#x79F0;</code></p>
<p>self[<code>&#x4E0B;&#x6807;&#x7D22;&#x5F15;</code>]</p>
<p>self(<code>&#x6784;&#x9020;&#x5668;&#x53C2;&#x6570;</code>)</p>
<p>self.init(<code>&#x6784;&#x9020;&#x5668;&#x53C2;&#x6570;</code>)</p>
</blockquote>
<p>&#x5982;&#x679C;&#x5728;&#x6784;&#x9020;&#x5668;&#x3001;&#x4E0B;&#x6807;&#x3001;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;<code>self</code> &#x5F15;&#x7528;&#x7684;&#x662F;&#x5F53;&#x524D;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;<code>self</code> &#x5F15;&#x7528;&#x7684;&#x662F;&#x5F53;&#x524D;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x5F53;&#x8BBF;&#x95EE;&#x6210;&#x5458;&#x65F6;&#xFF0C;<code>self</code> &#x53EF;&#x7528;&#x6765;&#x533A;&#x5206;&#x91CD;&#x540D;&#x53D8;&#x91CF;&#xFF0C;&#x4F8B;&#x5982;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span> </span>{
    <span class="hljs-keyword">var</span> greeting: <span class="hljs-type">String</span>
    <span class="hljs-keyword">init</span>(greeting: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.greeting = greeting
    }
}
</code></pre>
<p>&#x5728; <code>mutating</code> &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5BF9; <code>self</code> &#x91CD;&#x65B0;&#x8D4B;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Point</span> </span>{
    <span class="hljs-keyword">var</span> x = <span class="hljs-number">0.0</span>, y = <span class="hljs-number">0.0</span>
    <span class="hljs-keyword">mutating</span> <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">moveByX</span><span class="hljs-params">(deltaX: Double, y deltaY: Double)</span></span> {
        <span class="hljs-keyword">self</span> = <span class="hljs-type">Point</span>(x: x + deltaX, y: y + deltaY)
    }
}
</code></pre>
<blockquote>
<p>Self &#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="self-expression">self-expression </h4>
<blockquote>
<p><em>self &#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>self</strong>  | <a href="#self-method-expression"><em>self &#x65B9;&#x6CD5;&#x8868;&#x8FBE;&#x5F0F;</em></a> &#xFF5C; <a href="#self-subscript-expression"><em>self &#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em></a> | <a href="#self-initializer-expression"><em>self &#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h4 id="self-method-expression">self-method-expression </h4>
<blockquote>
<p><em>self &#x65B9;&#x6CD5;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>self</strong> <strong>.</strong> <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a></p>
</blockquote>
<h4 id="self-subscript-expression">self-subscript-expression </h4>
<blockquote>
<p><em>self &#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>self</strong> <strong>[</strong> <a href="#function-call-argument-list&#xAD;"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</em></a> <strong>]</strong></p>
</blockquote>
<h4 id="self-initializer-expression">self-initializer-expression </h4>
<blockquote>
<p><em>self &#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>self</strong> <strong>.</strong> <strong>init</strong></p>
</blockquote>
<h3 id="superclass-expression">&#x7236;&#x7C7B;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x7236;&#x7C7B;</em>&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x4F7F;&#x6211;&#x4EEC;&#x5728;&#x67D0;&#x4E2A;&#x7C7B;&#x4E2D;&#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x7236;&#x7C7B;&#xFF0C;&#x5B83;&#x6709;&#x5982;&#x4E0B;&#x5F62;&#x5F0F;&#xFF1A;</p>
<blockquote>
<p>super.<code>&#x6210;&#x5458;&#x540D;&#x79F0;</code></p>
<p>super[<code>&#x4E0B;&#x6807;&#x7D22;&#x5F15;</code>]</p>
<p>super.init(<code>&#x6784;&#x9020;&#x5668;&#x53C2;&#x6570;</code>)</p>
</blockquote>
<p>&#x7B2C;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#x7236;&#x7C7B;&#x7684;&#x67D0;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x7B2C;&#x4E8C;&#x79CD;&#x5F62;&#x5F0F;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#x7236;&#x7C7B;&#x7684;&#x4E0B;&#x6807;&#xFF0C;&#x7B2C;&#x4E09;&#x79CD;&#x5F62;&#x5F0F;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#x7236;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x5668;&#x3002;</p>
<p>&#x5B50;&#x7C7B;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7236;&#x7C7B;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x5B83;&#x4EEC;&#x7684;&#x6210;&#x5458;&#x3001;&#x4E0B;&#x6807;&#x548C;&#x6784;&#x9020;&#x5668;&#x4E2D;&#x4F7F;&#x7528;&#x7236;&#x7C7B;&#x4E2D;&#x7684;&#x5B9E;&#x73B0;&#x3002;</p>
<blockquote>
<p>&#x7236;&#x7C7B;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="superclass-expression">superclass-expression </h4>
<blockquote>
<p><em>&#x7236;&#x7C7B;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#superclass-method-expression"><em>&#x7236;&#x7C7B;&#x65B9;&#x6CD5;&#x8868;&#x8FBE;&#x5F0F;</em></a> | <a href="#superclass-subscript-expression"><em>&#x7236;&#x7C7B;&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em></a> | <a href="#superclass-initializer-expression"><em>&#x7236;&#x7C7B;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h4 id="superclass-method-expression">superclass-method-expression </h4>
<blockquote>
<p><em>&#x7236;&#x7C7B;&#x65B9;&#x6CD5;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>super</strong> <strong>.</strong> <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a></p>
</blockquote>
<h4 id="superclass-subscript-expression">superclass-subscript-expression </h4>
<blockquote>
<p><em>&#x7236;&#x7C7B;&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>super</strong> [<a href="#function-call-argument-list&#xAD;"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</em></a> <strong>]</strong></p>
</blockquote>
<h4 id="superclass-initializer-expression">superclass-initializer-expression </h4>
<blockquote>
<p><em>&#x7236;&#x7C7B;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>super</strong> <strong>.</strong> <strong>init</strong></p>
</blockquote>
<h3 id="closure-expression">&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;</em>&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#xFF0C;&#x5728;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x4E2D;&#x4E5F;&#x53EB; <em>lambda</em> &#x6216;<em>&#x533F;&#x540D;</em>&#x51FD;&#x6570;&#x3002;&#x8DDF;&#x51FD;&#x6570;&#x4E00;&#x6837;&#xFF0C;&#x95ED;&#x5305;&#x5305;&#x542B;&#x4E86;&#x5F85;&#x6267;&#x884C;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;&#x95ED;&#x5305;&#x8FD8;&#x4F1A;&#x6355;&#x83B7;&#x6240;&#x5728;&#x73AF;&#x5883;&#x4E2D;&#x7684;&#x5E38;&#x91CF;&#x548C;&#x53D8;&#x91CF;&#x3002;&#x5B83;&#x7684;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-swift">{ (parameters) -&gt; <span class="hljs-keyword">return</span> type <span class="hljs-keyword">in</span>
    statements
}
</code></pre>
<p>&#x95ED;&#x5305;&#x7684;&#x53C2;&#x6570;&#x58F0;&#x660E;&#x5F62;&#x5F0F;&#x8DDF;&#x51FD;&#x6570;&#x4E00;&#x6837;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="06_Declarations.html#function_declaration">&#x51FD;&#x6570;&#x58F0;&#x660E;</a>&#x3002;</p>
<p>&#x95ED;&#x5305;&#x8FD8;&#x6709;&#x51E0;&#x79CD;&#x7279;&#x6B8A;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x80FD;&#x8BA9;&#x95ED;&#x5305;&#x4F7F;&#x7528;&#x8D77;&#x6765;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#xFF1A;</p>
<ul>
<li>&#x95ED;&#x5305;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x548C;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5982;&#x679C;&#x7701;&#x7565;&#x4E86;&#x53C2;&#x6570;&#x540D;&#x548C;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x8981;&#x7701;&#x7565; <code>in</code> &#x5173;&#x952E;&#x5B57;&#x3002;&#x5982;&#x679C;&#x88AB;&#x7701;&#x7565;&#x7684;&#x7C7B;&#x578B;&#x65E0;&#x6CD5;&#x88AB;&#x7F16;&#x8BD1;&#x5668;&#x63A8;&#x65AD;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x5BFC;&#x81F4;&#x7F16;&#x8BD1;&#x9519;&#x8BEF;&#x3002;</li>
<li>&#x95ED;&#x5305;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x53C2;&#x6570;&#x540D;&#xFF0C;&#x53C2;&#x6570;&#x4F1A;&#x88AB;&#x9690;&#x5F0F;&#x547D;&#x540D;&#x4E3A; <code>$</code> &#x52A0;&#x4E0A;&#x5176;&#x7D22;&#x5F15;&#x4F4D;&#x7F6E;&#xFF0C;&#x4F8B;&#x5982; <code>$0</code>&#x3001;<code>$1</code>&#x3001;<code>$2</code> &#x5206;&#x522B;&#x8868;&#x793A;&#x7B2C;&#x4E00;&#x4E2A;&#x3001;&#x7B2C;&#x4E8C;&#x4E2A;&#x3001;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;</li>
<li>&#x5982;&#x679C;&#x95ED;&#x5305;&#x4E2D;&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x90A3;&#x4E48;&#x8BE5;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x4F1A;&#x88AB;&#x89C6;&#x4E3A;&#x95ED;&#x5305;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x8868;&#x8FBE;&#x5F0F;&#x7ED3;&#x679C;&#x7684;&#x7C7B;&#x578B;&#x4E5F;&#x4F1A;&#x88AB;&#x63A8;&#x65AD;&#x4E3A;&#x95ED;&#x5305;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x3002;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x51E0;&#x4E2A;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF1A;</p>
<pre><code class="lang-swift">myFunction {
    (x: <span class="hljs-type">Int</span>, y: <span class="hljs-type">Int</span>) -&gt; <span class="hljs-type">Int</span> <span class="hljs-keyword">in</span>
    <span class="hljs-keyword">return</span> x + y
}

myFunction {
    (x, y) <span class="hljs-keyword">in</span>
    <span class="hljs-keyword">return</span> x + y
}

myFunction { <span class="hljs-keyword">return</span> $<span class="hljs-number">0</span> + $<span class="hljs-number">1</span> }

myFunction { $<span class="hljs-number">0</span> + $<span class="hljs-number">1</span> }
</code></pre>
<p>&#x5173;&#x4E8E;&#x5982;&#x4F55;&#x5C06;&#x95ED;&#x5305;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x6765;&#x4F20;&#x9012;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="#function_call_expression">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;</a>&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x5FC5;&#x5C06;&#x5176;&#x5B58;&#x50A8;&#x5728;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x6765;&#x7ACB;&#x5373;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x4F20;&#x5165; <code>myFunction</code> &#x7684;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x5C31;&#x662F;&#x8FD9;&#x79CD;&#x7ACB;&#x5373;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x95ED;&#x5305;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x662F;&#x5426;&#x9003;&#x9038;&#x4E0E;&#x5176;&#x4F7F;&#x7528;&#x65F6;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x76F8;&#x5173;&#x3002;&#x4E00;&#x4E2A;&#x4F1A;&#x88AB;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x6216;&#x8005;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x975E;&#x9003;&#x9038;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7684;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x975E;&#x9003;&#x9038;&#x7684;&#xFF0C;&#x5426;&#x5219;&#xFF0C;&#x8FD9;&#x4E2A;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x9003;&#x9038;&#x7684;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x9003;&#x9038;&#x95ED;&#x5305;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="chapter2/07_Closures.md#escaping_closures">&#x9003;&#x9038;&#x95ED;&#x5305;</a>&#x3002;</p>
<h2 id="capture-lists">&#x6355;&#x83B7;&#x5217;&#x8868; </h2>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x95ED;&#x5305;&#x4F1A;&#x6355;&#x83B7;&#x9644;&#x8FD1;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x5E38;&#x91CF;&#x548C;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x5F3A;&#x5F15;&#x7528;&#x6307;&#x5411;&#x5B83;&#x4EEC;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;<em>&#x6355;&#x83B7;&#x5217;&#x8868;</em>&#x6765;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x5B83;&#x7684;&#x6355;&#x83B7;&#x884C;&#x4E3A;&#x3002;</p>
<p>&#x6355;&#x83B7;&#x5217;&#x8868;&#x5728;&#x53C2;&#x6570;&#x5217;&#x8868;&#x4E4B;&#x524D;&#xFF0C;&#x7531;&#x4E2D;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#xFF0C;&#x91CC;&#x9762;&#x662F;&#x7531;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#x4E00;&#x7CFB;&#x5217;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x4E00;&#x65E6;&#x4F7F;&#x7528;&#x4E86;&#x6355;&#x83B7;&#x5217;&#x8868;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x4F7F;&#x7528; <code>in</code> &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5373;&#x4F7F;&#x7701;&#x7565;&#x4E86;&#x53C2;&#x6570;&#x540D;&#x3001;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x548C;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x6355;&#x83B7;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x9879;&#x4F1A;&#x5728;&#x95ED;&#x5305;&#x521B;&#x5EFA;&#x65F6;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x3002;&#x6BCF;&#x4E00;&#x9879;&#x90FD;&#x4F1A;&#x7528;&#x95ED;&#x5305;&#x9644;&#x8FD1;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x540C;&#x540D;&#x5E38;&#x91CF;&#x6216;&#x8005;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x521D;&#x59CB;&#x5316;&#x3002;&#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6355;&#x83B7;&#x5217;&#x8868;&#x5305;&#x542B; <code>a</code> &#x800C;&#x4E0D;&#x5305;&#x542B; <code>b</code>&#xFF0C;&#x8FD9;&#x5C06;&#x5BFC;&#x81F4;&#x8FD9;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x5177;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x884C;&#x4E3A;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> a = <span class="hljs-number">0</span>
<span class="hljs-keyword">var</span> b = <span class="hljs-number">0</span>
<span class="hljs-keyword">let</span> closure = { [a] <span class="hljs-keyword">in</span>
    <span class="hljs-built_in">print</span>(a, b)
}

a = <span class="hljs-number">10</span>
b = <span class="hljs-number">10</span>
closure()
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;0 10&#x201D;</span>
</code></pre>
<p>&#x5728;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x53D8;&#x91CF; <code>b</code> &#x53EA;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x7136;&#x800C;&#xFF0C;&#x53D8;&#x91CF; <code>a</code> &#x6709;&#x4E24;&#x4E2A;&#xFF0C;&#x4E00;&#x4E2A;&#x5728;&#x95ED;&#x5305;&#x5916;&#xFF0C;&#x4E00;&#x4E2A;&#x5728;&#x95ED;&#x5305;&#x5185;&#x3002;&#x95ED;&#x5305;&#x5185;&#x7684;&#x53D8;&#x91CF; <code>a</code> &#x4F1A;&#x5728;&#x95ED;&#x5305;&#x521B;&#x5EFA;&#x65F6;&#x7528;&#x95ED;&#x5305;&#x5916;&#x7684;&#x53D8;&#x91CF; <code>a</code> &#x7684;&#x503C;&#x6765;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x9664;&#x6B64;&#x4E4B;&#x5916;&#x5B83;&#x4EEC;&#x5E76;&#x65E0;&#x5176;&#x4ED6;&#x8054;&#x7CFB;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5728;&#x95ED;&#x5305;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x6539;&#x53D8;&#x67D0;&#x4E2A; <code>a</code> &#x7684;&#x503C;&#x90FD;&#x4E0D;&#x4F1A;&#x5BF9;&#x53E6;&#x4E00;&#x4E2A; <code>a</code> &#x7684;&#x503C;&#x9020;&#x6210;&#x4EFB;&#x4F55;&#x5F71;&#x54CD;&#x3002;&#x4E0E;&#x6B64;&#x76F8;&#x53CD;&#xFF0C;&#x95ED;&#x5305;&#x5185;&#x5916;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF; <code>b</code>&#xFF0C;&#x56E0;&#x6B64;&#x5728;&#x95ED;&#x5305;&#x5916;&#x6539;&#x53D8;&#x5176;&#x503C;&#xFF0C;&#x95ED;&#x5305;&#x5185;&#x7684;&#x503C;&#x4E5F;&#x4F1A;&#x53D7;&#x5F71;&#x54CD;&#x3002;</p>
<p>&#x5982;&#x679C;&#x95ED;&#x5305;&#x6355;&#x83B7;&#x7684;&#x503C;&#x5177;&#x6709;&#x5F15;&#x7528;&#x8BED;&#x4E49;&#x5219;&#x6709;&#x6240;&#x4E0D;&#x540C;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6709;&#x4E24;&#x4E2A;&#x53D8;&#x91CF; <code>x</code>&#xFF0C;&#x4E00;&#x4E2A;&#x5728;&#x95ED;&#x5305;&#x5916;&#xFF0C;&#x4E00;&#x4E2A;&#x5728;&#x95ED;&#x5305;&#x5185;&#xFF0C;&#x7531;&#x4E8E;&#x5B83;&#x4EEC;&#x7684;&#x503C;&#x662F;&#x5F15;&#x7528;&#x8BED;&#x4E49;&#xFF0C;&#x867D;&#x7136;&#x8FD9;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x5B83;&#x4EEC;&#x5374;&#x90FD;&#x5F15;&#x7528;&#x7740;&#x540C;&#x4E00;&#x5B9E;&#x4F8B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SimpleClass</span> </span>{
    <span class="hljs-keyword">var</span> value: <span class="hljs-type">Int</span> = <span class="hljs-number">0</span>
}
<span class="hljs-keyword">var</span> x = <span class="hljs-type">SimpleClass</span>()
<span class="hljs-keyword">var</span> y = <span class="hljs-type">SimpleClass</span>()
<span class="hljs-keyword">let</span> closure = { [x] <span class="hljs-keyword">in</span>
    <span class="hljs-built_in">print</span>(x.value, y.value)
}

x.value = <span class="hljs-number">10</span>
y.value = <span class="hljs-number">10</span>
closure()
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;10 10&#x201D;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x6355;&#x83B7;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x503C;&#x662F;&#x7C7B;&#x7C7B;&#x578B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>weak</code> &#x6216;&#x8005; <code>unowned</code> &#x6765;&#x4FEE;&#x9970;&#x5B83;&#xFF0C;&#x95ED;&#x5305;&#x4F1A;&#x5206;&#x522B;&#x7528;&#x5F31;&#x5F15;&#x7528;&#x548C;&#x65E0;&#x4E3B;&#x5F15;&#x7528;&#x6765;&#x6355;&#x83B7;&#x8BE5;&#x503C;&#x3002;</p>
<pre><code class="lang-swift">myFunction { <span class="hljs-built_in">print</span>(<span class="hljs-keyword">self</span>.title) }                    <span class="hljs-comment">// &#x9690;&#x5F0F;&#x5F3A;&#x5F15;&#x7528;&#x6355;&#x83B7;</span>
myFunction { [<span class="hljs-keyword">self</span>] <span class="hljs-keyword">in</span> <span class="hljs-built_in">print</span>(<span class="hljs-keyword">self</span>.title) }             <span class="hljs-comment">// &#x663E;&#x5F0F;&#x5F3A;&#x5F15;&#x7528;&#x6355;&#x83B7;</span>
myFunction { [<span class="hljs-keyword">weak</span> <span class="hljs-keyword">self</span>] <span class="hljs-keyword">in</span> <span class="hljs-built_in">print</span>(<span class="hljs-keyword">self</span>!.title) }   <span class="hljs-comment">// &#x5F31;&#x5F15;&#x7528;&#x6355;&#x83B7;</span>
myFunction { [<span class="hljs-keyword">unowned</span> <span class="hljs-keyword">self</span>] <span class="hljs-keyword">in</span> <span class="hljs-built_in">print</span>(<span class="hljs-keyword">self</span>.title) } <span class="hljs-comment">// &#x65E0;&#x4E3B;&#x5F15;&#x7528;&#x6355;&#x83B7;</span>
</code></pre>
<p>&#x5728;&#x6355;&#x83B7;&#x5217;&#x8868;&#x4E2D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x4EFB;&#x610F;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x7ED1;&#x5B9A;&#x5230;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x4E0A;&#x3002;&#x8BE5;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5728;&#x95ED;&#x5305;&#x88AB;&#x521B;&#x5EFA;&#x65F6;&#x8FDB;&#x884C;&#x6C42;&#x503C;&#xFF0C;&#x95ED;&#x5305;&#x4F1A;&#x6309;&#x7167;&#x6307;&#x5B9A;&#x7684;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x6765;&#x6355;&#x83B7;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x4EE5;&#x5F31;&#x5F15;&#x7528;&#x6355;&#x83B7; self.parent &#x5E76;&#x8D4B;&#x503C;&#x7ED9; parent</span>
myFunction { [<span class="hljs-keyword">weak</span> parent = <span class="hljs-keyword">self</span>.parent] <span class="hljs-keyword">in</span> <span class="hljs-built_in">print</span>(parent!.title) }
</code></pre>
<p>&#x5173;&#x4E8E;&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x548C;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/07_Closures.html#closure_expressions">&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;</a>&#x3002;&#x5173;&#x4E8E;&#x6355;&#x83B7;&#x5217;&#x8868;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x548C;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/23_Automatic_Reference_Counting.html#resolving_strong_reference_cycles_for_closures">&#x89E3;&#x51B3;&#x95ED;&#x5305;&#x5F15;&#x8D77;&#x7684;&#x5FAA;&#x73AF;&#x5F3A;&#x5F15;&#x7528;</a>&#x3002;</p>
<blockquote>
<p>&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<h4 id="closure-expression">closure-expression </h4>
<p><em>&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>{</strong> <a href="#closure-signature"><em>&#x95ED;&#x5305;&#x7B7E;&#x540D;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#statements"><em>&#x8BED;&#x53E5;</em></a> <strong>}</strong></p>
<h4 id="closure-signature">closure-signature </h4>
<p>&#x95ED;&#x5305;&#x7B7E;&#x540D;<em> &#x2192; [</em>&#x53C2;&#x6570;&#x5B50;&#x53E5;<em>](#parameter-clause) [</em>&#x51FD;&#x6570;&#x7ED3;&#x679C;<em>](05_Declarations.md#function-result)<sub>&#x53EF;&#x9009;</sub> <em>*in</em></em></p>
<p><em>&#x95ED;&#x5305;&#x7B7E;&#x540D;</em> &#x2192; <a href="#identifier-list"><em>&#x6807;&#x8BC6;&#x7B26;&#x5217;&#x8868;</em></a> <a href="05_Declarations.md#function-result"><em>&#x51FD;&#x6570;&#x7ED3;&#x679C;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>in</strong></p>
<p><em>&#x95ED;&#x5305;&#x7B7E;&#x540D;</em> &#x2192; <a href="#capture-list"><em>&#x6355;&#x83B7;&#x5217;&#x8868;</em></a> <a href="05_Declarations.md#parameter-clause"><em>&#x53C2;&#x6570;&#x5B50;&#x53E5;</em></a> <a href="06_Declarations.html#function-result"><em>&#x51FD;&#x6570;&#x7ED3;&#x679C;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>in</strong></p>
<p><em>&#x95ED;&#x5305;&#x7B7E;&#x540D;</em> &#x2192; <a href="#capture-list"><em>&#x6355;&#x83B7;&#x5217;&#x8868;</em></a> <a href="02_Lexical_Structure.html#identifier-list"><em>&#x6807;&#x8BC6;&#x7B26;&#x5217;&#x8868;</em></a> <a href="06_Declarations.html#function-result"><em>&#x51FD;&#x6570;&#x7ED3;&#x679C;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>in</strong></p>
<p><em>&#x95ED;&#x5305;&#x7B7E;&#x540D;</em> &#x2192; <a href="#capture-list"><em>&#x6355;&#x83B7;&#x5217;&#x8868;</em></a> <strong>in</strong></p>
<h4 id="capture-list">capture-list </h4>
<p>&#x6355;&#x83B7;&#x5217;&#x8868;<em> &#x2192; [ [</em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;&#x5217;&#x8868;<em>](#capture-list-items) <em>*]</em></em></p>
<h4 id="capture-list-items">capture-list-items </h4>
<p><em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;&#x5217;&#x8868;</em> &#x2192; <a href="#capture-list-item"><em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;</em></a> | <a href="#capture-list-item"><em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;</em></a> <strong>,</strong> <a href="#capture-list-items"><em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;&#x5217;&#x8868;</em></a></p>
<h4 id="capture-list-item">capture-list-item </h4>
<p><em>&#x6355;&#x83B7;&#x5217;&#x8868;&#x9879;</em> &#x2192; <a href="#capture-specifier"><em>&#x6355;&#x83B7;&#x8BF4;&#x660E;&#x7B26;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<h4 id="capture-specifier">capture-specifier </h4>
<p><em>&#x6355;&#x83B7;&#x8BF4;&#x660E;&#x7B26;</em> &#x2192; <strong>weak</strong> | <strong>unowned</strong> | <strong>unowned(safe)</strong> | <strong>unowned(unsafe)</strong></p>
</blockquote>
<h3 id="implicit-member-expression">&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p>&#x82E5;&#x7C7B;&#x578B;&#x53EF;&#x88AB;&#x63A8;&#x65AD;&#x51FA;&#x6765;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<em>&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em>&#x6765;&#x8BBF;&#x95EE;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x6210;&#x5458;&#xFF08;&#x4F8B;&#x5982;&#x67D0;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x6216;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#x65B9;&#x6CD5;&#xFF09;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>.<code>&#x6210;&#x5458;&#x540D;&#x79F0;</code></p>
</blockquote>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> x = <span class="hljs-type">MyEnumeration</span>.<span class="hljs-type">SomeValue</span>
x = .<span class="hljs-type">AnotherValue</span>
</code></pre>
<blockquote>
<p>&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="implicit-member-expression">implicit-member-expression </h4>
<blockquote>
<p><em>&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>.</strong> <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a></p>
</blockquote>
<h3 id="parenthesized-expression">&#x5706;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x5706;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;</em>&#x662F;&#x7531;&#x5706;&#x62EC;&#x53F7;&#x5305;&#x56F4;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x5706;&#x62EC;&#x53F7;&#x8BF4;&#x660E;&#x6210;&#x7EC4;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5148;&#x540E;&#x64CD;&#x4F5C;&#x3002;&#x6210;&#x7EC4;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7C7B;&#x578B; - &#x4F8B;&#x5982; <code>(1)</code> &#x7684;&#x7C7B;&#x578B;&#x5C31;&#x662F;&#x7B80;&#x5355;&#x7684; <code>Int</code>&#x3002;</p>
<blockquote>
<p>&#x5706;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="parenthesized-expression">parenthesized-expression </h4>
<blockquote>
<p><em>&#x5706;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>( <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> )</strong></p>
</blockquote>
<h3 id="Tuple-Expression">&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;</em>&#x7531;&#x5706;&#x62EC;&#x53F7;&#x548C;&#x5176;&#x4E2D;&#x591A;&#x4E2A;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#x3002;&#x6BCF;&#x4E2A;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x524D;&#x9762;&#x53EF;&#x4EE5;&#x6709;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x7528;&#x5192;&#x53F7;&#x9694;&#x5F00;&#x3002;&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>(<code>&#x6807;&#x8BC6;&#x7B26; 1</code> : <code>&#x8868;&#x8FBE;&#x5F0F; 1</code>, <code>&#x6807;&#x8BC6;&#x7B26; 2</code> : <code>&#x8868;&#x8FBE;&#x5F0F; 2</code>, <code>...</code>)</p>
</blockquote>
<p>&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x90FD;&#x6CA1;&#x6709;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4E24;&#x4E2A;&#x6216;&#x662F;&#x66F4;&#x591A;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5355;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7528;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x5C31;&#x662F;&#x62EC;&#x53F7;&#x8868;&#x8FBE;&#x5F0F;&#x4E86;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5728; Swift &#x4E2D;&#xFF0C;&#x7A7A;&#x7684;&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x7A7A;&#x7684;&#x5143;&#x7EC4;&#x7C7B;&#x578B;&#x90FD;&#x5199;&#x4F5C; <code>()</code>&#x3002;&#x7531;&#x4E8E; <code>Void</code> &#x662F; <code>()</code> &#x7684;&#x7C7B;&#x578B;&#x522B;&#x540D;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x8868;&#x793A;&#x7A7A;&#x7684;&#x5143;&#x7EC4;&#x7C7B;&#x578B;&#x3002;&#x867D;&#x7136;&#x5982;&#x6B64;&#xFF0C;<code>Void</code> &#x5C31;&#x50CF;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x578B;&#x522B;&#x540D;&#x4E00;&#x6837;&#xFF0C;&#x6C38;&#x8FDC;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x2014;&#x2014;&#x4E0D;&#x80FD;&#x8868;&#x793A;&#x7A7A;&#x7684;&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="tuple-expression">tuple-expression </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>( )</strong> | <strong>(</strong><a href="#tuple-element"><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;</em></a>&#xFF0C; <a href="#tuple-element-list"><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;&#x5217;&#x8868;</em></a> <strong>)</strong></p>
</blockquote>
<h4 id="tuple-element-list">tuple-element-list </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;&#x5217;&#x8868;</em> &#x2192; <a href="#tuple-element"><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;</em></a> | <a href="#tuple-element"><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;</em></a> <strong>,</strong> <a href="#tuple-element-list"><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="tuple-element">tuple-element </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x5143;&#x7D20;</em> &#x2192; <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> | <a href="identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a> <strong>:</strong> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h3 id="wildcard-expression">&#x901A;&#x914D;&#x7B26;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x901A;&#x914D;&#x7B26;&#x8868;&#x8FBE;&#x5F0F;</em>&#x53EF;&#x4EE5;&#x5728;&#x8D4B;&#x503C;&#x8FC7;&#x7A0B;&#x4E2D;&#x663E;&#x5F0F;&#x5FFD;&#x7565;&#x67D0;&#x4E2A;&#x503C;&#x3002;&#x4F8B;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;<code>10</code> &#x88AB;&#x8D4B;&#x503C;&#x7ED9; <code>x</code>&#xFF0C;&#x800C; <code>20</code> &#x5219;&#x88AB;&#x5FFD;&#x7565;&#xFF1A;</p>
<pre><code class="lang-swift">(x, <span class="hljs-number">_</span>) = (<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)
<span class="hljs-comment">// x &#x4E3A; 10&#xFF0C;20 &#x88AB;&#x5FFD;&#x7565;</span>
</code></pre>
<blockquote>
<p>&#x901A;&#x914D;&#x7B26;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="wildcard-expression">wildcard-expression </h4>
<blockquote>
<p><em>&#x901A;&#x914D;&#x7B26;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>_</strong></p>
</blockquote>
<h3 id="key-path-expression">Key-path &#x8868;&#x8FBE;&#x5F0F; </h3>
<p>Key-path &#x8868;&#x8FBE;&#x5F0F;&#x5F15;&#x7528;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x5C5E;&#x6027;&#x6216;&#x4E0B;&#x6807;&#x3002;&#x5728;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x4E2D;&#x4F7F;&#x573A;&#x666F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; Key-path &#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#x89C2;&#x5BDF;&#x952E;&#x503C;&#x5BF9;&#x3002;&#x683C;&#x5F0F;&#x4E3A;&#xFF1A;</p>
<blockquote>
<p><strong>\&#x7C7B;&#x578B;&#x540D;.&#x8DEF;&#x5F84;</strong></p>
</blockquote>
<p><em>&#x7C7B;&#x578B;&#x540D;</em>&#x662F;&#x4E00;&#x4E2A;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x5305;&#x542B;&#x4EFB;&#x4F55;&#x6CDB;&#x578B;&#x53C2;&#x6570;&#xFF0C;&#x4F8B;&#x5982; <code>String</code>&#x3001;<code>[Int]</code> &#x6216; <code>Set&lt;Int&gt;</code>&#x3002;</p>
<p><em>&#x8DEF;&#x5F84;</em>&#x53EF;&#x7531;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x3001;&#x4E0B;&#x6807;&#x3001;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x8005;&#x5F3A;&#x5236;&#x89E3;&#x5305;&#x8868;&#x8FBE;&#x5F0F;&#x7EC4;&#x6210;&#x3002;&#x4EE5;&#x4E0A;&#x4EFB;&#x610F; key-path &#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x4EE5;&#x4EFB;&#x4F55;&#x987A;&#x5E8F;&#x91CD;&#x590D;&#x591A;&#x6B21;&#x3002;</p>
<p>&#x5728;&#x7F16;&#x8BD1;&#x671F;&#xFF0C;key-path &#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x88AB;&#x4E00;&#x4E2A; <a href="https://developer.apple.com/documentation/swift/keypath" target="_blank">KeyPath</a> &#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x66FF;&#x6362;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x6240;&#x6709;&#x7C7B;&#x578B;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F20;&#x9012; key-path &#x53C2;&#x6570;&#x5230;&#x4E0B;&#x6807;&#x65B9;&#x6CD5; <code>subscript(keyPath:)</code> &#x6765;&#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x503C;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">SomeStructure</span> </span>{
    <span class="hljs-keyword">var</span> someValue: <span class="hljs-type">Int</span>
}

<span class="hljs-keyword">let</span> s = <span class="hljs-type">SomeStructure</span>(someValue: <span class="hljs-number">12</span>)
<span class="hljs-keyword">let</span> pathToProperty = \<span class="hljs-type">SomeStructure</span>.someValue

<span class="hljs-keyword">let</span> value = s[keyPath: pathToProperty]
<span class="hljs-comment">// &#x503C;&#x4E3A; 12</span>
</code></pre>
<p>&#x5728;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7C7B;&#x578B;&#x63A8;&#x65AD;&#x6765;&#x786E;&#x5B9A;&#x6240;&#x8BBF;&#x95EE;&#x7684;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565; key-path &#x524D;&#x7684;&#x7C7B;&#x578B;&#x540D;&#x5B57;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F7F;&#x7528; <code>\.someProperty</code> &#x4EE3;&#x66FF;&#x4E86; <code>SomeClass.someProperty</code> &#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span>: <span class="hljs-title">NSObject</span> </span>{
    <span class="hljs-meta">@objc</span> <span class="hljs-keyword">var</span> someProperty: <span class="hljs-type">Int</span>
    <span class="hljs-keyword">init</span>(someProperty: <span class="hljs-type">Int</span>) {
        <span class="hljs-keyword">self</span>.someProperty = someProperty
    }
}

<span class="hljs-keyword">let</span> <span class="hljs-built_in">c</span> = <span class="hljs-type">SomeClass</span>(someProperty: <span class="hljs-number">10</span>)
<span class="hljs-built_in">c</span>.observe(\.someProperty) { object, change <span class="hljs-keyword">in</span>
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x4F7F;&#x7528; <code>self</code> &#x4F5C;&#x4E3A;&#x8DEF;&#x5F84;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6052;&#x7B49; key path (<code>\.self</code>)&#x3002;&#x6052;&#x7B49; key path &#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x6574;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x56E0;&#x6B64;&#x4F60;&#x4EC5;&#x9700;&#x4E00;&#x6B65;&#x64CD;&#x4F5C;&#x4FBF;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x5B83;&#x6765;&#x8BBF;&#x95EE;&#x4EE5;&#x53CA;&#x4FEE;&#x6539;&#x5176;&#x5B58;&#x50A8;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> compoundValue = (a: <span class="hljs-number">1</span>, b: <span class="hljs-number">2</span>)
<span class="hljs-comment">// &#x7B49;&#x4EF7;&#x4E8E; compoundValue = (a: 10, b: 20)</span>
compoundValue[keyPath: \.<span class="hljs-keyword">self</span>] = (a: <span class="hljs-number">10</span>, b: <span class="hljs-number">20</span>)
</code></pre>
<p>&#x901A;&#x8FC7;&#x70B9;&#x8BED;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x8DEF;&#x5F84;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#xFF0C;&#x4EE5;&#x6B64;&#x6765;&#x8BBF;&#x95EE;&#x67D0;&#x5B9E;&#x4F8B;&#x7684;&#x5C5E;&#x6027;&#x7684;&#x5C5E;&#x6027;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x4F7F;&#x7528; key-path &#x8868;&#x8FBE;&#x5F0F; <code>\OuterStructure.outer.someValue</code> &#x6765;&#x8BBF;&#x95EE; <code>OuterStructure</code> &#x7C7B;&#x578B;&#x4E2D; <code>outer</code> &#x5C5E;&#x6027;&#x7684; <code>someValue</code> &#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">OuterStructure</span> </span>{
    <span class="hljs-keyword">var</span> outer: <span class="hljs-type">SomeStructure</span>
    <span class="hljs-keyword">init</span>(someValue: <span class="hljs-type">Int</span>) {
        <span class="hljs-keyword">self</span>.outer = <span class="hljs-type">SomeStructure</span>(someValue: someValue)
    }
}

<span class="hljs-keyword">let</span> nested = <span class="hljs-type">OuterStructure</span>(someValue: <span class="hljs-number">24</span>)
<span class="hljs-keyword">let</span> nestedKeyPath = \<span class="hljs-type">OuterStructure</span>.outer.someValue

<span class="hljs-keyword">let</span> nestedValue = nested[keyPath: nestedKeyPath]
<span class="hljs-comment">// nestedValue &#x7684;&#x503C;&#x4E3A; 24</span>
</code></pre>
<p>&#x8DEF;&#x5F84;&#x4E2D;&#x4E5F;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4F7F;&#x7528;&#x4E2D;&#x62EC;&#x53F7;&#x7684;&#x4E0B;&#x6807;&#x8BBF;&#x95EE;&#xFF0C;&#x53EA;&#x8981;&#x4E0B;&#x6807;&#x8BBF;&#x95EE;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x6EE1;&#x8DB3; <code>Hashable</code> &#x534F;&#x8BAE;&#x5373;&#x53EF;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x5728; key path &#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x4E0B;&#x6807;&#x6765;&#x8BBF;&#x95EE;&#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x5143;&#x7D20;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> greetings = [<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-string">&quot;hola&quot;</span>, <span class="hljs-string">&quot;bonjour&quot;</span>, <span class="hljs-string">&quot;&#xC548;&#xB155;&quot;</span>]
<span class="hljs-keyword">let</span> myGreeting = greetings[keyPath: \[<span class="hljs-type">String</span>].[<span class="hljs-number">1</span>]]
<span class="hljs-comment">// myGreeting &#x7684;&#x503C;&#x4E3A; &apos;hola&apos;</span>
</code></pre>
<p>&#x4E0B;&#x6807;&#x8BBF;&#x95EE;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6216;&#x8005;&#x5B57;&#x9762;&#x91CF;&#xFF0C;&#x5E76;&#x4E14; key-path &#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x4F7F;&#x7528;&#x503C;&#x8BED;&#x4E49;&#x6765;&#x6355;&#x83B7;&#x6B64;&#x503C;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5728; key-path &#x8868;&#x8FBE;&#x5F0F;&#x548C;&#x95ED;&#x5305;&#x4E2D;&#x90FD;&#x4F7F;&#x7528;&#x4E86; <code>index</code> &#x53D8;&#x91CF;&#x6765;&#x8BBF;&#x95EE; <code>greetings</code> &#x6570;&#x7EC4;&#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x5143;&#x7D20;&#x3002;&#x5F53; <code>index</code> &#x88AB;&#x4FEE;&#x6539;&#x65F6;&#xFF0C;key-path &#x8868;&#x8FBE;&#x5F0F;&#x4ECD;&#x65E7;&#x5F15;&#x7528;&#x6570;&#x7EC4;&#x7B2C;&#x4E09;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x800C;&#x95ED;&#x5305;&#x5219;&#x4F7F;&#x7528;&#x4E86;&#x65B0;&#x7684;&#x7D22;&#x5F15;&#x503C;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> index = <span class="hljs-number">2</span>
<span class="hljs-keyword">let</span> path = \[<span class="hljs-type">String</span>].[index]
<span class="hljs-keyword">let</span> fn: ([<span class="hljs-type">String</span>]) -&gt; <span class="hljs-type">String</span> = { strings <span class="hljs-keyword">in</span> strings[index] }

<span class="hljs-built_in">print</span>(greetings[keyPath: path])
<span class="hljs-comment">// &#x6253;&#x5370; &quot;bonjour&quot;</span>
<span class="hljs-built_in">print</span>(fn(greetings))
<span class="hljs-comment">// &#x6253;&#x5370; &quot;bonjour&quot;</span>

<span class="hljs-comment">// &#x5C06; &apos;index&apos; &#x8BBE;&#x7F6E;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x503C;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230; &apos;path&apos;</span>
index += <span class="hljs-number">1</span>
<span class="hljs-built_in">print</span>(greetings[keyPath: path])
<span class="hljs-comment">// &#x6253;&#x5370; &quot;bonjour&quot;</span>

<span class="hljs-comment">// &apos;fn&apos; &#x95ED;&#x5305;&#x4F7F;&#x7528;&#x4E86;&#x65B0;&#x503C;&#x3002;</span>
<span class="hljs-built_in">print</span>(fn(greetings))
<span class="hljs-comment">// &#x6253;&#x5370; &quot;&#xC548;&#xB155;&quot;</span>
</code></pre>
<p>&#x8DEF;&#x5F84;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x548C;&#x5F3A;&#x5236;&#x89E3;&#x5305;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5728; key path &#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x53EF;&#x9009;&#x94FE;&#x6765;&#x8BBF;&#x95EE;&#x53EF;&#x9009;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> firstGreeting: <span class="hljs-type">String</span>? = greetings.first
<span class="hljs-built_in">print</span>(firstGreeting?.<span class="hljs-built_in">count</span> <span class="hljs-keyword">as</span> <span class="hljs-type">Any</span>)
<span class="hljs-comment">// &#x6253;&#x5370; &quot;Optional(5)&quot;</span>

<span class="hljs-comment">// &#x4F7F;&#x7528; key path &#x5B9E;&#x73B0;&#x540C;&#x6837;&#x7684;&#x529F;&#x80FD;</span>
<span class="hljs-keyword">let</span> <span class="hljs-built_in">count</span> = greetings[keyPath: \[<span class="hljs-type">String</span>].first?.<span class="hljs-built_in">count</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">count</span> <span class="hljs-keyword">as</span> <span class="hljs-type">Any</span>)
<span class="hljs-comment">// &#x6253;&#x5370; &quot;Optional(5)&quot;</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x6DF7;&#x5408;&#x4F7F;&#x7528;&#x5404;&#x79CD; key-path &#x7EC4;&#x4EF6;&#x6765;&#x8BBF;&#x95EE;&#x4E00;&#x4E9B;&#x6DF1;&#x5EA6;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x901A;&#x8FC7;&#x7EC4;&#x5408;&#x4E0D;&#x540C;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x4F7F;&#x7528; key-path &#x8868;&#x8FBE;&#x5F0F;&#x8BBF;&#x95EE;&#x4E86;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x6570;&#x7EC4;&#x4E2D;&#x4E0D;&#x540C;&#x7684;&#x503C;&#x548C;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> interestingNumbers = [<span class="hljs-string">&quot;prime&quot;</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">11</span>, <span class="hljs-number">13</span>, <span class="hljs-number">17</span>],
                          <span class="hljs-string">&quot;triangular&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-number">21</span>, <span class="hljs-number">28</span>],
                          <span class="hljs-string">&quot;hexagonal&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">6</span>, <span class="hljs-number">15</span>, <span class="hljs-number">28</span>, <span class="hljs-number">45</span>, <span class="hljs-number">66</span>, <span class="hljs-number">91</span>]]
<span class="hljs-built_in">print</span>(interestingNumbers[keyPath: \[<span class="hljs-type">String</span>: [<span class="hljs-type">Int</span>]].[<span class="hljs-string">&quot;prime&quot;</span>]] <span class="hljs-keyword">as</span> <span class="hljs-type">Any</span>)
<span class="hljs-comment">// &#x6253;&#x5370; &quot;Optional([2, 3, 5, 7, 11, 13, 17])&quot;</span>
<span class="hljs-built_in">print</span>(interestingNumbers[keyPath: \[<span class="hljs-type">String</span>: [<span class="hljs-type">Int</span>]].[<span class="hljs-string">&quot;prime&quot;</span>]![<span class="hljs-number">0</span>]])
<span class="hljs-comment">// &#x6253;&#x5370; &quot;2&quot;</span>
<span class="hljs-built_in">print</span>(interestingNumbers[keyPath: \[<span class="hljs-type">String</span>: [<span class="hljs-type">Int</span>]].[<span class="hljs-string">&quot;hexagonal&quot;</span>]!.<span class="hljs-built_in">count</span>])
<span class="hljs-comment">// &#x6253;&#x5370; &quot;7&quot;</span>
<span class="hljs-built_in">print</span>(interestingNumbers[keyPath: \[<span class="hljs-type">String</span>: [<span class="hljs-type">Int</span>]].[<span class="hljs-string">&quot;hexagonal&quot;</span>]!.<span class="hljs-built_in">count</span>.bitWidth])
<span class="hljs-comment">// &#x6253;&#x5370; &quot;64&quot;</span>
</code></pre>
<p>&#x5173;&#x4E8E;&#x66F4;&#x591A;&#x5982;&#x4F55;&#x4F7F;&#x7528; key path &#x4E0E; Objective-C APIs &#x4EA4;&#x4E92;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift" target="_blank">&#x5728; Swift &#x4E2D;&#x4F7F;&#x7528; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x7279;&#x6027;</a>&#x3002;&#x5173;&#x4E8E;&#x66F4;&#x591A; key-value &#x7F16;&#x7A0B;&#x548C; key-value &#x89C2;&#x5BDF;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i" target="_blank">Key-Value &#x7F16;&#x7A0B;</a> &#x548C; <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177i" target="_blank">Key-Value &#x89C2;&#x5BDF;&#x7F16;&#x7A0B;</a>&#x3002;</p>
<blockquote>
<p>key-path &#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<h4 id="key-path-expression">key-path-expression </h4>
<p><em>key-path &#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>\</strong>  <a href="03_Types.html#type">&#x7C7B;&#x578B;</a><sub>&#x53EF;&#x9009;</sub>  <strong>.</strong>  <a href="#key-path-components">&#x591A;&#x4E2A; key-path &#x7EC4;&#x4EF6;</a></p>
<h4 id="key-path-components">key-path-components </h4>
<p><em>&#x591A;&#x4E2A; key-path &#x7EC4;&#x4EF6;</em> &#x2192; <a href="#key-path-component">key-path &#x7EC4;&#x4EF6;</a> | <a href="#key-path-component">key-path &#x7EC4;&#x4EF6;</a> <strong>.</strong> <a href="#key-path-components">&#x591A;&#x4E2A; key-path &#x7EC4;&#x4EF6;</a></p>
<h4 id="key-path-component">key-path-component </h4>
<p><em>key-path &#x7EC4;&#x4EF6;</em> &#x2192;  <a href="02_Lexical_Structure.html#identifier">&#x6807;&#x8BC6;&#x7B26;</a> <a href="#key-path-postfixes">&#x591A;&#x4E2A; key-path &#x540E;&#x7F00;</a><sub>&#x53EF;&#x9009;<sub> | <a href="#key-path-postfixes">&#x591A;&#x4E2A; key-path &#x540E;&#x7F00;</a></sub></sub></p>
<h4 id="key-path-postfixes">key-path-postfixes </h4>
<h4 id="key-path-postfixes"><em>&#x591A;&#x4E2A; key-path &#x540E;&#x7F00;</em> &#x2192;  <a href="#key-path-postfix">key-path &#x540E;&#x7F00;</a> <a href="#key-path-postfixes">&#x591A;&#x4E2A; key-path &#x540E;&#x7F00;</a><sub>&#x53EF;&#x9009;<sub> key-path-postfixes </sub></sub></h4>
<p><em>key-path &#x540E;&#x7F00;</em> &#x2192; <strong>?</strong> | <strong>!</strong> | <strong>self</strong> | <strong>[</strong> <a href="#function-call-argument-list">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</a> <strong>]</strong> </p>
</blockquote>
<h3 id="selector-expression">&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em>&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x901A;&#x8FC7;&#x9009;&#x62E9;&#x5668;&#x6765;&#x5F15;&#x7528;&#x5728; Objective-C &#x4E2D;&#x65B9;&#x6CD5;&#xFF08;method&#xFF09;&#x548C;&#x5C5E;&#x6027;&#xFF08;property&#xFF09;&#x7684; setter &#x548C; getter &#x65B9;&#x6CD5;&#x3002;</p>
<blockquote>
<p>#selector(&#x65B9;&#x6CD5;&#x540D;)</p>
<p>#selector(getter: &#x5C5E;&#x6027;&#x540D;)
#selector(setter: &#x5C5E;&#x6027;&#x540D;)</p>
</blockquote>
<p>&#x65B9;&#x6CD5;&#x540D;&#x548C;&#x5C5E;&#x6027;&#x540D;&#x5FC5;&#x987B;&#x662F;&#x5B58;&#x5728;&#x4E8E; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x548C;&#x5C5E;&#x6027;&#x7684;&#x5F15;&#x7528;&#x3002;&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E00;&#x4E2A; Selector &#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span>: <span class="hljs-title">NSObject</span> </span>{
    <span class="hljs-keyword">let</span> property: <span class="hljs-type">String</span>
    <span class="hljs-meta">@objc</span>(doSomethingWithInt:)
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">doSomething</span><span class="hljs-params">(<span class="hljs-number">_</span> x: Int)</span></span> { }

    <span class="hljs-keyword">init</span>(property: <span class="hljs-type">String</span>) {
        <span class="hljs-keyword">self</span>.property = property
    }
}
<span class="hljs-keyword">let</span> selectorForMethod = #selector(<span class="hljs-type">SomeClass</span>.doSomething(<span class="hljs-number">_</span>:))
<span class="hljs-keyword">let</span> selectorForPropertyGetter = #selector(getter: <span class="hljs-type">SomeClass</span>.property)
</code></pre>
<p>&#x5F53;&#x4E3A;&#x5C5E;&#x6027;&#x7684; getter &#x521B;&#x5EFA;&#x9009;&#x62E9;&#x5668;&#x65F6;&#xFF0C;&#x5C5E;&#x6027;&#x540D;&#x53EF;&#x4EE5;&#x662F;&#x53D8;&#x91CF;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x5E38;&#x91CF;&#x5C5E;&#x6027;&#x7684;&#x5F15;&#x7528;&#x3002;&#x4F46;&#x662F;&#x5F53;&#x4E3A;&#x5C5E;&#x6027;&#x7684; setter &#x521B;&#x5EFA;&#x9009;&#x62E9;&#x5668;&#x65F6;&#xFF0C;&#x5C5E;&#x6027;&#x540D;&#x53EA;&#x53EF;&#x4EE5;&#x662F;&#x5BF9;&#x53D8;&#x91CF;&#x5C5E;&#x6027;&#x7684;&#x5F15;&#x7528;&#x3002;</p>
<p>&#x65B9;&#x6CD5;&#x540D;&#x79F0;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x5706;&#x62EC;&#x53F7;&#x6765;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#xFF0C;&#x5E76;&#x4F7F;&#x7528; as &#x64CD;&#x4F5C;&#x7B26;&#x6765;&#x533A;&#x5206;&#x5177;&#x6709;&#x76F8;&#x540C;&#x65B9;&#x6CD5;&#x540D;&#x4F46;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4F8B;&#x5982;:</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">SomeClass</span> </span>{
    <span class="hljs-meta">@objc</span>(doSomethingWithString:)
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">doSomething</span><span class="hljs-params">(<span class="hljs-number">_</span> x: String)</span></span> { }
}
<span class="hljs-keyword">let</span> anotherSelector = #selector(<span class="hljs-type">SomeClass</span>.doSomething(<span class="hljs-number">_</span>:) <span class="hljs-keyword">as</span> (<span class="hljs-type">SomeClass</span>) -&gt; (<span class="hljs-type">String</span>) -&gt; <span class="hljs-type">Void</span>)
</code></pre>
<p>&#x7531;&#x4E8E;&#x9009;&#x62E9;&#x5668;&#x662F;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x521B;&#x5EFA;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x7F16;&#x8BD1;&#x5668;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x4EE5;&#x53CA;&#x662F;&#x5426;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x66B4;&#x9732;&#x7ED9;&#x4E86; Objective-C &#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x867D;&#x7136;&#x65B9;&#x6CD5;&#x540D;&#x6216;&#x8005;&#x5C5E;&#x6027;&#x540D;&#x662F;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4E0D;&#x4F1A;&#x88AB;&#x6C42;&#x503C;&#x3002;</p>
</blockquote>
<p>&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x5982;&#x4F55;&#x5728; Swift &#x4EE3;&#x7801;&#x4E2D;&#x4F7F;&#x7528;&#x9009;&#x62E9;&#x5668;&#x6765;&#x4E0E; Objective-C API &#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift" target="_blank">&#x5728; Swift &#x4E2D;&#x4F7F;&#x7528; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x7279;&#x6027;</a>&#x3002;</p>
<blockquote>
<p>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="selector-expression">selector-expression </h4>
<blockquote>
<p><em>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>#selector</strong> <strong>(</strong> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>)</strong></p>
<p><em>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>#selector</strong> <strong>(</strong> <a href="#expression"><em>getter:&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>)</strong></p>
<p><em>&#x9009;&#x62E9;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>#selector</strong> <strong>(</strong> <a href="#expression"><em>setter:&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>)</strong></p>
</blockquote>
<h2 id="key-path-string-expressions">Key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F; </h2>
<p>key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x5F15;&#x7528; Objective-C &#x5C5E;&#x6027;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x901A;&#x5E38;&#x5728; key-value &#x7F16;&#x7A0B;&#x548C; key-value &#x89C2;&#x5BDF; APIs &#x4E2D;&#x4F7F;&#x7528;&#x3002;&#x5176;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>#keyPath</code> ( <code>&#x5C5E;&#x6027;&#x540D;</code> )</p>
</blockquote>
<p>&#x5C5E;&#x6027;&#x540D;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5728; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x5C5E;&#x6027;&#x7684;&#x5F15;&#x7528;&#x3002;&#x5728;&#x7F16;&#x8BD1;&#x671F;&#xFF0C;key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x88AB;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x91CF;&#x66FF;&#x6362;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span>: <span class="hljs-title">NSObject</span> </span>{
    <span class="hljs-meta">@objc</span> <span class="hljs-keyword">var</span> someProperty: <span class="hljs-type">Int</span>
    <span class="hljs-keyword">init</span>(someProperty: <span class="hljs-type">Int</span>) {
        <span class="hljs-keyword">self</span>.someProperty = someProperty
    }
}

<span class="hljs-keyword">let</span> <span class="hljs-built_in">c</span> = <span class="hljs-type">SomeClass</span>(someProperty: <span class="hljs-number">12</span>)
<span class="hljs-keyword">let</span> keyPath = #keyPath(<span class="hljs-type">SomeClass</span>.someProperty)

<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> value = <span class="hljs-built_in">c</span>.value(forKey: keyPath) {
    <span class="hljs-built_in">print</span>(value)
}
<span class="hljs-comment">// &#x6253;&#x5370; &quot;12&quot;</span>
</code></pre>
<p>&#x5F53;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x4E2D;&#x4F7F;&#x7528; key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x7C7B;&#x540D;&#xFF0C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x5C5E;&#x6027;&#x540D;&#x6765;&#x8BBF;&#x95EE;&#x8FD9;&#x4E2A;&#x7C7B;&#x7684;&#x67D0;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">extension</span> <span class="hljs-title">SomeClass</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">getSomeKeyPath</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">String</span> {
&gt; 
        <span class="hljs-keyword">return</span> #keyPath(someProperty)
    }
}
<span class="hljs-built_in">print</span>(keyPath == <span class="hljs-built_in">c</span>.getSomeKeyPath())
<span class="hljs-comment">// &#x6253;&#x5370; &quot;true&quot;</span>
</code></pre>
<p>&#x7531;&#x4E8E; key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x7F16;&#x8BD1;&#x671F;&#x624D;&#x521B;&#x5EFA;&#xFF0C;&#x7F16;&#x8BD1;&#x671F;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x5C5E;&#x6027;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF0C;&#x4EE5;&#x53CA;&#x5C5E;&#x6027;&#x662F;&#x5426;&#x66B4;&#x9732;&#x7ED9; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x66F4;&#x591A;&#x5982;&#x4F55;&#x4F7F;&#x7528; key path &#x4E0E; Objective-C APIs &#x4EA4;&#x4E92;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https:/developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift">&#x5728; Swift &#x4E2D;&#x4F7F;&#x7528; Objective-C &#x8FD0;&#x884C;&#x65F6;&#x7279;&#x6027;</a>&#x3002;&#x5173;&#x4E8E;&#x66F4;&#x591A; key-value &#x7F16;&#x7A0B;&#x548C; key-value &#x89C2;&#x5BDF;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.md#//apple_ref/doc/uid/10000107i" target="_blank">Key-Value &#x7F16;&#x7A0B;</a> &#x548C; <a href="https:/developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.md#//apple_ref/doc/uid/10000177i">Key-Value &#x89C2;&#x5BDF;&#x7F16;&#x7A0B;</a>&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;</p>
<p>&#x5C3D;&#x7BA1;<em>&#x5C5E;&#x6027;&#x540D;</em>&#x662F;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F46;&#x5B83;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x88AB;&#x6C42;&#x503C;</p>
<p>key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<h4 id="key-path-string-expression">key-path-string-expression </h4>
<p><em>key-path &#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <strong>#keyPath (</strong> <a href="#expression">&#x8868;&#x8FBE;&#x5F0F;</a>  <strong>)</strong></p>
</blockquote>
<h2 id="postfix-expressions">&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F; </h2>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em>&#x5C31;&#x662F;&#x5728;&#x67D0;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x540E;&#x9762;&#x8FD0;&#x7528;&#x540E;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;&#x6216;&#x5176;&#x4ED6;&#x540E;&#x7F00;&#x8BED;&#x6CD5;&#x3002;&#x4ECE;&#x8BED;&#x6CD5;&#x6784;&#x6210;&#x4E0A;&#x6765;&#x770B;&#xFF0C;&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x662F;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x8FD9;&#x4E9B;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/02_Basic_Operators.html">&#x57FA;&#x672C;&#x8FD0;&#x7B97;&#x7B26;</a> &#x548C; <a href="../chapter2/26_Advanced_Operators.html">&#x9AD8;&#x7EA7;&#x8FD0;&#x7B97;&#x7B26;</a>&#x3002;</p>
<p>&#x5173;&#x4E8E; Swift &#x6807;&#x51C6;&#x5E93;&#x63D0;&#x4F9B;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="https://developer.apple.com/documentation/swift/operator_declarations" target="_blank">&#x8FD0;&#x7B97;&#x7B26;&#x5B9A;&#x4E49;</a>&#x3002;</p>
<blockquote>
<p>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="postfix-expression">postfix-expression </h4>
<blockquote>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#primary-expression"><em>&#x57FA;&#x672C;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="02_Lexical_Structure.html#postfix-operator"><em>&#x540E;&#x7F00;&#x8FD0;&#x7B97;&#x7B26;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#function-call-expression"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#initializer-expression"><em>&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#explicit-member-expression"><em>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-self-expression"><em>&#x540E;&#x7F00; self &#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#dynamic-type-expression"><em>dynamicType &#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#subscript-expression"><em>&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#forced-value-expression"><em>&#x5F3A;&#x5236;&#x53D6;&#x503C;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#optional-chaining-expression"><em>&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h3 id="function-call-expression">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;</em>&#x7531;&#x51FD;&#x6570;&#x540D;&#x548C;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7EC4;&#x6210;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x51FD;&#x6570;&#x540D;</code>(<code>&#x53C2;&#x6570; 1</code>, <code>&#x53C2;&#x6570; 2</code>)</p>
</blockquote>
<p>&#x51FD;&#x6570;&#x540D;&#x53EF;&#x4EE5;&#x662F;&#x503C;&#x4E3A;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x7684;&#x4EFB;&#x610F;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#x5982;&#x679C;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x4E2D;&#x6307;&#x5B9A;&#x4E86;&#x53C2;&#x6570;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x90A3;&#x4E48;&#x5728;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x4E5F;&#x5FC5;&#x987B;&#x5F97;&#x5199;&#x51FA;&#x6765;&#x3002;&#x8FD9;&#x79CD;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x5177;&#x6709;&#x4EE5;&#x4E0B;&#x5F62;&#x5F0F;&#xFF1A;</p>
<blockquote>
<p><code>&#x51FD;&#x6570;&#x540D;</code>(<code>&#x53C2;&#x6570;&#x540D; 1</code>: <code>&#x53C2;&#x6570; 1</code>, <code>&#x53C2;&#x6570;&#x540D; 2</code>: <code>&#x53C2;&#x6570; 2</code>)</p>
</blockquote>
<p>&#x5982;&#x679C;&#x51FD;&#x6570;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5C3E;&#x90E8;&#xFF08;&#x53F3;&#x5706;&#x62EC;&#x53F7;&#x4E4B;&#x540E;&#xFF09;&#x52A0;&#x4E0A;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#xFF0C;&#x8BE5;&#x95ED;&#x5305;&#x4F1A;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x3002;&#x5982;&#x4E0B;&#x4E24;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// someFunction &#x63A5;&#x53D7;&#x6574;&#x6570;&#x548C;&#x95ED;&#x5305;&#x53C2;&#x6570;</span>
someFunction(x, f: {$<span class="hljs-number">0</span> == <span class="hljs-number">13</span>})
someFunction(x) {$<span class="hljs-number">0</span> == <span class="hljs-number">13</span>}
</code></pre>
<p>&#x5982;&#x679C;&#x95ED;&#x5305;&#x662F;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x552F;&#x4E00;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x5706;&#x62EC;&#x53F7;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// someFunction &#x53EA;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x95ED;&#x5305;&#x53C2;&#x6570;</span>
myData.someMethod() {$<span class="hljs-number">0</span> == <span class="hljs-number">13</span>}
myData.someMethod {$<span class="hljs-number">0</span> == <span class="hljs-number">13</span>}
</code></pre>
<blockquote>
<p>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
<h4 id="function-call-expression">function-call-expression </h4>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="#function-call-argument-clause"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x5B50;&#x53E5;</em></a></p>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <a href="#function-call-argument-clause"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x5B50;&#x53E5;</em></a><sub>&#x53EF;&#x9009;</sub> <a href="#trailing-closure"><em>&#x5C3E;&#x968F;&#x95ED;&#x5305;</em></a></p>
<h4 id="function-call-argument-clause">function-call-argument-clause </h4>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x5B50;&#x53E5;</em> &#x2192; <strong>(</strong>  <strong>)</strong>  | <strong>(</strong> <a href="#function-call-argument-list"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</em></a> <strong>)</strong></p>
<h4 id="function-call-argument-list">function-call-argument-list </h4>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</em> &#x2192; <a href="#function-call-argument">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;</a> | <a href="#function-call-argument">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;</a> <strong>,</strong> <a href="#function-call-argument-list"><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;&#x8868;</em></a></p>
<h4 id="function-call-argument">function-call-argument </h4>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;</em> &#x2192; <a href="#expression">&#x8868;&#x8FBE;&#x5F0F;</a> | <a href="02_Lexical_Structure.html#identifier">&#x6807;&#x8BC6;&#x7B26;</a> <strong>:</strong> <a href="#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
<p><em>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x53C2;&#x6570;</em> &#x2192; <a href="02_Lexical_Structure.html#operator">&#x8FD0;&#x7B97;&#x7B26;</a> | <a href="02_Lexical_Structure.html#identifier">&#x6807;&#x8BC6;&#x7B26;</a> <strong>:</strong> <a href="02_Lexical_Structure.html#operator"><em>&#x8FD0;&#x7B97;&#x7B26;</em></a></p>
<h4 id="trailing-closure">trailing-closure </h4>
<p><em>&#x5C3E;&#x968F;&#x95ED;&#x5305;</em> &#x2192; <a href="#closure-expression"><em>&#x95ED;&#x5305;&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>
<h3 id="initializer-expression">&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em>&#x7528;&#x4E8E;&#x8BBF;&#x95EE;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>.init(<code>&#x6784;&#x9020;&#x5668;&#x53C2;&#x6570;</code>)</p>
</blockquote>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x521D;&#x59CB;&#x5316;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#x7684;&#x65B0;&#x5B9E;&#x4F8B;&#x3002;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x4EE3;&#x7406;&#x7ED9;&#x7236;&#x7C7B;&#x6784;&#x9020;&#x5668;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeSubClass</span>: <span class="hljs-title">SomeSuperClass</span> </span>{
    <span class="hljs-keyword">override</span> <span class="hljs-keyword">init</span>() {
        <span class="hljs-comment">// &#x6B64;&#x5904;&#x4E3A;&#x5B50;&#x7C7B;&#x6784;&#x9020;&#x8FC7;&#x7A0B;</span>
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>()
    }
}
</code></pre>
<p>&#x548C;&#x51FD;&#x6570;&#x7C7B;&#x4F3C;&#xFF0C;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x503C;&#x3002; &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x56E0;&#x4E3A; String &#x7C7B;&#x578B;&#x6709;&#x591A;&#x79CD;&#x6784;&#x9020;&#x5668;</span>
<span class="hljs-keyword">let</span> initializer: <span class="hljs-type">Int</span> -&gt; <span class="hljs-type">String</span> = <span class="hljs-type">String</span>.<span class="hljs-keyword">init</span>
<span class="hljs-keyword">let</span> oneTwoThree = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>].<span class="hljs-built_in">map</span>(initializer).<span class="hljs-built_in">reduce</span>(<span class="hljs-string">&quot;&quot;</span>, combine: +)
<span class="hljs-built_in">print</span>(oneTwoThree)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;123&#x201D;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x901A;&#x8FC7;&#x540D;&#x5B57;&#x6765;&#x6307;&#x5B9A;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x7528;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x800C;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x6784;&#x9020;&#x5668;&#x3002;&#x5728;&#x5176;&#x4ED6;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> s1 = <span class="hljs-type">SomeType</span>.<span class="hljs-keyword">init</span>(data: <span class="hljs-number">3</span>) <span class="hljs-comment">// &#x6709;&#x6548;</span>
<span class="hljs-keyword">let</span> s2 = <span class="hljs-type">SomeType</span>(data: <span class="hljs-number">1</span>)      <span class="hljs-comment">// &#x6709;&#x6548;</span>

<span class="hljs-keyword">let</span> s4 = someValue.<span class="hljs-keyword">dynamicType</span>(data: <span class="hljs-number">5</span>)      <span class="hljs-comment">// &#x9519;&#x8BEF;</span>
<span class="hljs-keyword">let</span> s3 = someValue.<span class="hljs-keyword">dynamicType</span>.<span class="hljs-keyword">init</span>(data: <span class="hljs-number">7</span>) <span class="hljs-comment">// &#x6709;&#x6548;</span>
</code></pre>
<blockquote>
<p>&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="initializer-expression">initializer-expression </h4>
<blockquote>
<p><em>&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> <strong>init</strong></p>
<p><em>&#x6784;&#x9020;&#x5668;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> <strong>init</strong> <strong>(</strong> <a href="#argument-names"><em>&#x53C2;&#x6570;&#x540D;&#x79F0;</em></a> <strong>)</strong></p>
</blockquote>
<h3 id="explicit-member-expression">&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em>&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x8BBF;&#x95EE;&#x547D;&#x540D;&#x7C7B;&#x578B;&#x3001;&#x5143;&#x7EC4;&#x6216;&#x8005;&#x6A21;&#x5757;&#x7684;&#x6210;&#x5458;&#xFF0C;&#x5176;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>.<code>&#x6210;&#x5458;&#x540D;</code></p>
</blockquote>
<p>&#x547D;&#x540D;&#x7C7B;&#x578B;&#x7684;&#x67D0;&#x4E2A;&#x6210;&#x5458;&#x5728;&#x539F;&#x59CB;&#x5B9E;&#x73B0;&#x6216;&#x8005;&#x6269;&#x5C55;&#x4E2D;&#x5B9A;&#x4E49;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span> </span>{
    <span class="hljs-keyword">var</span> someProperty = <span class="hljs-number">42</span>
}
<span class="hljs-keyword">let</span> <span class="hljs-built_in">c</span> = <span class="hljs-type">SomeClass</span>()
<span class="hljs-keyword">let</span> y = <span class="hljs-built_in">c</span>.someProperty <span class="hljs-comment">// &#x8BBF;&#x95EE;&#x6210;&#x5458;</span>
</code></pre>
<p>&#x5143;&#x7EC4;&#x7684;&#x6210;&#x5458;&#x4F1A;&#x9690;&#x5F0F;&#x5730;&#x6839;&#x636E;&#x8868;&#x793A;&#x5B83;&#x4EEC;&#x51FA;&#x73B0;&#x987A;&#x5E8F;&#x7684;&#x6574;&#x6570;&#x6765;&#x547D;&#x540D;&#xFF0C;&#x4EE5; 0 &#x5F00;&#x59CB;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> t = (<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)
t.<span class="hljs-number">0</span> = t.<span class="hljs-number">1</span>
<span class="hljs-comment">// &#x73B0;&#x5728;&#x5143;&#x7EC4; t &#x4E3A; (20, 20, 30)</span>
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x6A21;&#x5757;&#x7684;&#x6210;&#x5458;&#x6765;&#x8BF4;&#xFF0C;&#x53EA;&#x80FD;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x9876;&#x7EA7;&#x58F0;&#x660E;&#x4E2D;&#x7684;&#x6210;&#x5458;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>dynamicMemberLookup</code> &#x5C5E;&#x6027;&#x58F0;&#x660E;&#x7684;&#x7C7B;&#x578B;&#x5305;&#x542B;&#x53EF;&#x4EE5;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x67E5;&#x627E;&#x7684;&#x6210;&#x5458;&#xFF0C;&#x5177;&#x4F53;&#x8BF7;&#x53C2;&#x9605; <a href="07_Attributes.html">&#x5C5E;&#x6027;</a>&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x533A;&#x5206;&#x53EA;&#x6709;&#x53C2;&#x6570;&#x540D;&#x6709;&#x6240;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x5728;&#x5706;&#x62EC;&#x53F7;&#x4E2D;&#x5199;&#x51FA;&#x53C2;&#x6570;&#x540D;&#xFF0C;&#x53C2;&#x6570;&#x540D;&#x540E;&#x7D27;&#x8DDF;&#x4E00;&#x4E2A;&#x5192;&#x53F7;&#xFF0C;&#x5BF9;&#x4E8E;&#x6CA1;&#x6709;&#x53C2;&#x6570;&#x540D;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4F7F;&#x7528;&#x4E0B;&#x5212;&#x7EBF;&#x4EE3;&#x66FF;&#x53C2;&#x6570;&#x540D;&#x3002;&#x800C;&#x5BF9;&#x4E8E;&#x91CD;&#x8F7D;&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x9700;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x8FDB;&#x884C;&#x533A;&#x5206;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SomeClass</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">someMethod</span><span class="hljs-params">(x: Int, y: Int)</span></span> {}
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">someMethod</span><span class="hljs-params">(x: Int, z: Int)</span></span> {}
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">overloadedMethod</span><span class="hljs-params">(x: Int, y: Int)</span></span> {}
    <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">overloadedMethod</span><span class="hljs-params">(x: Int, y: Bool)</span></span> {}
}
<span class="hljs-keyword">let</span> instance = <span class="hljs-type">SomeClass</span>()

<span class="hljs-keyword">let</span> a = instance.someMethod              <span class="hljs-comment">// &#x6709;&#x6B67;&#x4E49;</span>
<span class="hljs-keyword">let</span> b = instance.someMethod(<span class="hljs-number">_</span>:y:)        <span class="hljs-comment">// &#x65E0;&#x6B67;&#x4E49;</span>

<span class="hljs-keyword">let</span> d = instance.overloadedMethod        <span class="hljs-comment">// &#x6709;&#x6B67;&#x4E49;</span>
<span class="hljs-keyword">let</span> d = instance.overloadedMethod(<span class="hljs-number">_</span>:y:)  <span class="hljs-comment">// &#x6709;&#x6B67;&#x4E49;</span>
<span class="hljs-keyword">let</span> d: (<span class="hljs-type">Int</span>, <span class="hljs-type">Bool</span>) -&gt; <span class="hljs-type">Void</span>  = instance.overloadedMethod(<span class="hljs-number">_</span>:y:)  <span class="hljs-comment">// &#x65E0;&#x6B67;&#x4E49;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x70B9;&#x53F7;&#xFF08;<code>.</code>&#xFF09;&#x51FA;&#x73B0;&#x5728;&#x884C;&#x9996;&#xFF0C;&#x5B83;&#x4F1A;&#x88AB;&#x89C6;&#x4E3A;&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x9690;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x3002;&#x4F8B;&#x5982;&#x5982;&#x4E0B;&#x4EE3;&#x7801;&#x6240;&#x5C55;&#x793A;&#x7684;&#x88AB;&#x5206;&#x4E3A;&#x591A;&#x884C;&#x7684;&#x94FE;&#x5F0F;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> x = [<span class="hljs-number">10</span>, <span class="hljs-number">3</span>, <span class="hljs-number">20</span>, <span class="hljs-number">15</span>, <span class="hljs-number">4</span>]
    .<span class="hljs-built_in">sort</span>()
    .<span class="hljs-built_in">filter</span> { $<span class="hljs-number">0</span> &gt; <span class="hljs-number">5</span> }
    .<span class="hljs-built_in">map</span> { $<span class="hljs-number">0</span> * <span class="hljs-number">100</span> }
</code></pre>
<blockquote>
<p>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="explicit-member-expression">explicit-member-expression </h4>
<blockquote>
<p><em>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> [<em>&#x5341;&#x8FDB;&#x5236;&#x6570;&#x5B57;</em>]    (02_Lexical_Structure.md#decimal-digit)</p>
<p><em>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a> <a href="09_Generic_Parameters_and_Arguments.html#generic-argument-clause"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;</em></a><sub>&#x53EF;&#x9009;</sub><br></p>
<p><em>&#x663E;&#x5F0F;&#x6210;&#x5458;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> [<em>&#x6807;&#x8BC6;&#x7B26;</em>]    (02_Lexical_Structure.md#identifier) <strong>(</strong> <a href="#argument-names"><em>&#x53C2;&#x6570;&#x540D;&#x79F0;</em></a> <strong>)</strong></p>
</blockquote>
<h4 id="argument-names">argument-names </h4>
<blockquote>
<p><em>&#x53C2;&#x6570;&#x540D;&#x79F0;</em> &#x2192; <a href="#argument-name"><em>&#x53C2;&#x6570;&#x540D;</em></a> <a href="#argument-names"><em>&#x53C2;&#x6570;&#x540D;&#x79F0;</em></a><sub>&#x53EF;&#x9009;</sub><br></p>
</blockquote>
<h4 id="argument-name">argument-name </h4>
<blockquote>
<p><em>&#x53C2;&#x6570;&#x540D;</em> &#x2192; <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a> <strong>:</strong></p>
</blockquote>
<h3 id="postfix-self-expression">&#x540E;&#x7F00; self &#x8868;&#x8FBE;&#x5F0F; </h3>
<p>&#x540E;&#x7F00; <code>self</code> &#x8868;&#x8FBE;&#x5F0F;&#x7531;&#x67D0;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x7C7B;&#x578B;&#x540D;&#x7D27;&#x8DDF; <code>.self</code> &#x7EC4;&#x6210;&#xFF0C;&#x5176;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>.self</p>
<p><code>&#x7C7B;&#x578B;</code>.self</p>
</blockquote>
<p>&#x7B2C;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x4F8B;&#x5982;&#xFF1A;<code>x.self</code> &#x8FD4;&#x56DE; <code>x</code>&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x79CD;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x76F8;&#x5E94;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6765;&#x83B7;&#x53D6;&#x67D0;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x7C7B;&#x578B;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x503C;&#x6765;&#x4F7F;&#x7528;&#x3002;&#x4F8B;&#x5982;&#xFF0C;<code>SomeClass.self</code> &#x4F1A;&#x8FD4;&#x56DE; <code>SomeClass</code> &#x7C7B;&#x578B;&#x672C;&#x8EAB;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x5176;&#x4F20;&#x9012;&#x7ED9;&#x76F8;&#x5E94;&#x51FD;&#x6570;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;</p>
<blockquote>
<p>&#x540E;&#x7F00; self &#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="postfix-self-expression">postfix-self-expression </h4>
<blockquote>
<p><em>&#x540E;&#x7F00; self &#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>.</strong> <strong>self</strong></p>
</blockquote>
<h3 id="subscript-expression">&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p>&#x53EF;&#x901A;&#x8FC7;<em>&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em>&#x8BBF;&#x95EE;&#x76F8;&#x5E94;&#x7684;&#x4E0B;&#x6807;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>[<code>&#x7D22;&#x5F15;&#x8868;&#x8FBE;&#x5F0F;</code>]</p>
</blockquote>
<p>&#x8981;&#x83B7;&#x53D6;&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF0C;&#x53EF;&#x5C06;&#x7D22;&#x5F15;&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x4E3A;&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x53C2;&#x6570;&#x6765;&#x8C03;&#x7528;&#x4E0B;&#x6807; getter&#x3002;&#x4E0B;&#x6807; setter &#x7684;&#x8C03;&#x7528;&#x65B9;&#x5F0F;&#x4E0E;&#x4E4B;&#x4E00;&#x6837;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x4E0B;&#x6807;&#x7684;&#x58F0;&#x660E;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="06_Declarations.html#protocol_subscript_declaration">&#x534F;&#x8BAE;&#x4E0B;&#x6807;&#x58F0;&#x660E;</a>&#x3002;</p>
<blockquote>
<p>&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="subscript-expression">subscript-expression </h4>
<blockquote>
<p><em>&#x4E0B;&#x6807;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>[</strong> <a href="#expression-list"><em>&#x8868;&#x8FBE;&#x5F0F;&#x5217;&#x8868;</em></a> <strong>]</strong></p>
</blockquote>
<h3 id="forced-Value-expression">&#x5F3A;&#x5236;&#x53D6;&#x503C;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p>&#x5F53;&#x4F60;&#x786E;&#x5B9A;&#x53EF;&#x9009;&#x503C;&#x4E0D;&#x662F; <code>nil</code> &#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<em>&#x5F3A;&#x5236;&#x53D6;&#x503C;&#x8868;&#x8FBE;&#x5F0F;</em>&#x6765;&#x5F3A;&#x5236;&#x89E3;&#x5305;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>!</p>
</blockquote>
<p>&#x5982;&#x679C;&#x8BE5;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x4E0D;&#x662F; <code>nil</code>&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x89E3;&#x5305;&#x540E;&#x7684;&#x503C;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x629B;&#x51FA;&#x8FD0;&#x884C;&#x65F6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x88AB;&#x4FEE;&#x6539;&#xFF0C;&#x65E0;&#x8BBA;&#x662F;&#x4FEE;&#x6539;&#x503C;&#x672C;&#x8EAB;&#xFF0C;&#x8FD8;&#x662F;&#x4FEE;&#x6539;&#x503C;&#x7684;&#x6210;&#x5458;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> x: <span class="hljs-type">Int</span>? = <span class="hljs-number">0</span>
x!++
<span class="hljs-comment">// x &#x73B0;&#x5728;&#x662F; 1</span>

<span class="hljs-keyword">var</span> someDictionary = [<span class="hljs-string">&quot;a&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-string">&quot;b&quot;</span>: [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>]]
someDictionary[<span class="hljs-string">&quot;a&quot;</span>]![<span class="hljs-number">0</span>] = <span class="hljs-number">100</span>
<span class="hljs-comment">// someDictionary &#x73B0;&#x5728;&#x662F; [b: [10, 20], a: [100, 2, 3]]</span>
</code></pre>
<blockquote>
<p>&#x5F3A;&#x5236;&#x53D6;&#x503C;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="forced-value-expression">forced-value-expression </h4>
<blockquote>
<p><em>&#x5F3A;&#x5236;&#x53D6;&#x503C;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>!</strong></p>
</blockquote>
<h3 id="optional-chaining-expression">&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F; </h3>
<p><em>&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;</em>&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x503C;&#x7684;&#x4FBF;&#x6377;&#x65B9;&#x6CD5;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p><code>&#x8868;&#x8FBE;&#x5F0F;</code>?</p>
</blockquote>
<p>&#x540E;&#x7F00; <code>?</code> &#x8FD0;&#x7B97;&#x7B26;&#x4F1A;&#x6839;&#x636E;&#x8868;&#x8FBE;&#x5F0F;&#x751F;&#x6210;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x800C;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;</p>
<p>&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x5305;&#x542B;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x7684;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4F1A;&#x6BD4;&#x8F83;&#x7279;&#x6B8A;&#x3002;&#x5982;&#x679C;&#x8BE5;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x662F; <code>nil</code>&#xFF0C;&#x6574;&#x4E2A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x76F4;&#x63A5;&#x8FD4;&#x56DE; <code>nil</code>&#x3002;&#x5982;&#x679C;&#x8BE5;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x4E0D;&#x662F; <code>nil</code>&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x89E3;&#x5305;&#x540E;&#x7684;&#x503C;&#xFF0C;&#x5E76;&#x5C06;&#x8BE5;&#x503C;&#x7528;&#x4E8E;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x5269;&#x4F59;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x5728;&#x8FD9;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6574;&#x4E2A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x90FD;&#x4F1A;&#x662F;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x5305;&#x542B;&#x4E86;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x6709;&#x6700;&#x5916;&#x5C42;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5982;&#x679C; <code>c</code> &#x4E0D;&#x662F; <code>nil</code>&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x7684;&#x503C;&#x4F1A;&#x88AB;&#x89E3;&#x5305;&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7; <code>.property</code> &#x8BBF;&#x95EE;&#x5B83;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x63A5;&#x7740;&#x8FDB;&#x4E00;&#x6B65;&#x901A;&#x8FC7; <code>.performAction()</code> &#x8C03;&#x7528;&#x76F8;&#x5E94;&#x65B9;&#x6CD5;&#x3002;&#x6574;&#x4E2A; <code>c?.property.performAction()</code> &#x8868;&#x8FBE;&#x5F0F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x591A;&#x91CD;&#x53EF;&#x9009;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> <span class="hljs-built_in">c</span>: <span class="hljs-type">SomeClass</span>?
<span class="hljs-keyword">var</span> result: <span class="hljs-type">Bool</span>? = <span class="hljs-built_in">c</span>?.property.performAction()
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x8DDF;&#x4E0B;&#x9762;&#x7684;&#x4E0D;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x4F8B;&#x5B50;&#x7B49;&#x4EF7;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">var</span> result: <span class="hljs-type">Bool</span>? = <span class="hljs-literal">nil</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> unwrappedC = <span class="hljs-built_in">c</span> {
    result = unwrappedC.property.performAction()
}
</code></pre>
<p>&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x89E3;&#x5305;&#x540E;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x88AB;&#x4FEE;&#x6539;&#xFF0C;&#x65E0;&#x8BBA;&#x662F;&#x4FEE;&#x6539;&#x503C;&#x672C;&#x8EAB;&#xFF0C;&#x8FD8;&#x662F;&#x4FEE;&#x6539;&#x503C;&#x7684;&#x6210;&#x5458;&#x3002;&#x5982;&#x679C;&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x4E3A; <code>nil</code>&#xFF0C;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x53F3;&#x4FA7;&#x7684;&#x8D4B;&#x503C;&#x64CD;&#x4F5C;&#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">someFunctionWithSideEffects</span><span class="hljs-params">()</span></span> -&gt; <span class="hljs-type">Int</span> {
    <span class="hljs-comment">// &#x8BD1;&#x8005;&#x6CE8;&#xFF1A;&#x4E3A;&#x4E86;&#x80FD;&#x770B;&#x51FA;&#x6B64;&#x51FD;&#x6570;&#x662F;&#x5426;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x52A0;&#x4E0A;&#x4E86;&#x4E00;&#x53E5;&#x6253;&#x5370;</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;someFunctionWithSideEffects&quot;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-number">42</span>
}
<span class="hljs-keyword">var</span> someDictionary = [<span class="hljs-string">&quot;a&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-string">&quot;b&quot;</span>: [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>]]

someDictionary[<span class="hljs-string">&quot;not here&quot;</span>]?[<span class="hljs-number">0</span>] = someFunctionWithSideEffects()
<span class="hljs-comment">// someFunctionWithSideEffects &#x4E0D;&#x4F1A;&#x88AB;&#x6267;&#x884C;</span>
<span class="hljs-comment">// someDictionary &#x4F9D;&#x7136;&#x662F; [&quot;b&quot;: [10, 20], &quot;a&quot;: [1, 2, 3]]</span>

someDictionary[<span class="hljs-string">&quot;a&quot;</span>]?[<span class="hljs-number">0</span>] = someFunctionWithSideEffects()
<span class="hljs-comment">// someFunctionWithSideEffects &#x88AB;&#x6267;&#x884C;&#x5E76;&#x8FD4;&#x56DE; 42</span>
<span class="hljs-comment">// someDictionary &#x73B0;&#x5728;&#x662F; [&quot;b&quot;: [10, 20], &quot;a&quot;: [42, 2, 3]]</span>
</code></pre>
<blockquote>
<p>&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="optional-chaining-expression">optional-chaining-expression </h4>
<blockquote>
<p><em>&#x53EF;&#x9009;&#x94FE;&#x8868;&#x8FBE;&#x5F0F;</em> &#x2192; <a href="#postfix-expression"><em>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</em></a> <strong>?</strong></p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="03_Types.html" class="navigation navigation-prev " aria-label="Previous page: 类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="05_Statements.html" class="navigation navigation-next " aria-label="Next page: 语句">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"表达式","level":"1.4.4","depth":2,"next":{"title":"语句","level":"1.4.5","depth":2,"path":"chapter3/05_Statements.md","ref":"chapter3/05_Statements.md","articles":[]},"previous":{"title":"类型","level":"1.4.3","depth":2,"path":"chapter3/03_Types.md","ref":"chapter3/03_Types.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter3/04_Expressions.md","mtime":"2019-07-05T01:01:08.239Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

